Skip to content

Commit

Permalink
feat: 🎸 add Azure OpenAI support (#10)
Browse files Browse the repository at this point in the history
add Azure OpenAI support

✅ Closes: #9
  • Loading branch information
bingryan authored Apr 21, 2023
1 parent 8676926 commit 0e6f839
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/config/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"assistantContentType": "markdown"
},
"chatgptInfo": {
"openai_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"api_key": "",
"temperature": "0.5",
"top_p": "1.0",
"presence_penalty": "0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare namespace App {
}

interface chatgptInfo {
openai_key: string;
api_key: string;
temperature: string;
top_p: string;
presence_penalty: string;
Expand Down
10 changes: 10 additions & 0 deletions src/utils/misc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function parseDomain(url: string) {
const { hostname } = new URL(url);
const domain = hostname.split('.').slice(-2).join('.');
const tld = hostname.split('.').slice(-1).join('');
return {
hostname,
domain,
tld,
};
}
23 changes: 18 additions & 5 deletions src/views/chat/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Spin } from '@arco-design/web-vue';
import { useChatStore } from '@/store';
import Textarea from '@/components/Textarea/index.vue';
import { parseDomain } from '@/utils/misc';
import { useCopyCode } from './hooks/useCopyCode';
import { useChat } from './hooks/useChat';
import { useScroll } from './hooks/useScroll';
Expand Down Expand Up @@ -111,6 +112,20 @@
}
}
function handleRequestHeader(apiUrl: string, apiKey: string) {
const { domain } = parseDomain(apiUrl);
if (domain === 'azure.com') {
return {
'api-key': apiKey,
'Content-Type': 'application/json',
};
}
return {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};
}
async function handleConversation() {
const message = prompt.value;
Expand Down Expand Up @@ -147,15 +162,13 @@
const {
engine,
api_url: apiUrl,
openai_key: openaiKey,
api_key: apiKey,
} = chatSetting.value.setting;
const messages = cacheContextList.value.slice(0, -1);
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${openaiKey}`,
'Content-Type': 'application/json',
},
// @ts-ignore
headers: handleRequestHeader(apiUrl, apiKey),
body: JSON.stringify({
messages,
model: engine,
Expand Down
8 changes: 4 additions & 4 deletions src/views/setting/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@
<Collapse>
<CollapseItem :header="$t('settings.chatgptConfig')">
<FormItem
field="openaiKey"
label="OpenAIKey:"
field="apiKey"
label="apiKey:"
:validate-trigger="['change', 'input']">
<InputPassword
v-model="settingform.chatgptInfo.openai_key"
placeholder="your Open AI Key..." />
v-model="settingform.chatgptInfo.api_key"
placeholder="your Azure/Open AI Key..." />
</FormItem>
<FormItem field="temperature" label="temperature" feedback>
<Input
Expand Down
8 changes: 4 additions & 4 deletions src/views/siderbar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
placeholder="New CHat" />
</FormItem>
<FormItem
field="openaiKey"
label="OpenAIKey:"
field="apiKey"
label="apiKey:"
:validate-trigger="['change', 'input']">
<InputPassword
v-model="chatInfo.setting.openai_key"
placeholder="your Open AI Key..." />
v-model="chatInfo.setting.api_key"
placeholder="your Azure/Open AI Key..." />
</FormItem>
<FormItem field="temperature" label="temperature" feedback>
<Input v-model="chatInfo.setting.temperature" default-value="0.5" />
Expand Down

0 comments on commit 0e6f839

Please sign in to comment.