Skip to content

Commit

Permalink
feat: 支持设置host
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghonglu.neo committed Mar 24, 2023
1 parent 5d97e20 commit af8ae9a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src-tauri/src/app/cmd/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use eventsource_stream::{Eventsource, EventStreamError};
use serde_json::{json, Value};
use serde::{ser::Serializer, Serialize, Deserialize};
use futures::{TryStreamExt};
use std::{ time::Duration };
use std::{ time::Duration, env::consts::OS };
use log::{error, info};

type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -56,6 +56,7 @@ pub struct Message {
#[allow(non_snake_case)]
pub struct FetchOption {
pub proxy: Option<String>,
pub host: String,
pub apiKey: String,
pub model: String,
pub temperature: f32,
Expand All @@ -69,8 +70,7 @@ pub async fn fetch_chat_api(
option: FetchOption,
) -> Result<u64> {
// https://platform.openai.com/docs/guides/chat/introduction
// "https://api.openai.com/v1/chat/completions";
let url = "https://api.openai.com/v1/chat/completions";
// let url = "https://api.openai.com/v1/chat/completions";
let data = json!({
"model": option.model,
"messages": messages,
Expand All @@ -89,9 +89,10 @@ pub async fn fetch_chat_api(
}
client_builder.build().unwrap()
};
let res = client.post(url)
let res = client.post(option.host)
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", option.apiKey))
.header(reqwest::header::USER_AGENT, format!("ChatGPT-Tauri ({})", OS))
.timeout(Duration::from_secs(600))
.body(data.to_string())
.send()
Expand Down
21 changes: 20 additions & 1 deletion src/components/common/Setting/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const model = ref({
avatar: userInfo.value.avatar,
apiKey: userConfig.value.apiKey,
modelName: userConfig.value.modelName,
host: userConfig.value.host,
proxy: userConfig.value.proxy,
})
Expand Down Expand Up @@ -65,6 +66,21 @@ const rules: FormRules = {
trigger: ['input', 'blur'],
},
],
host: [
{
required: true,
message: '请输入openai api host',
validator(rule: FormItemRule, value: string) {
if (!value)
return new Error('不能为空')
else if (!/^https:\/\/\S+$/.test(value))
return new Error('请输入正确的host')
return true
},
trigger: ['input', 'blur'],
},
],
}
function saveUserInfo() {
Expand All @@ -75,7 +91,7 @@ function saveUserInfo() {
userConfig.value.apiKey = model.value.apiKey
userConfig.value.modelName = model.value.modelName
userConfig.value.proxy = model.value.proxy
userConfig.value.host = model.value.host
userStore.recordState()
ms.success(t('common.success'))
}
Expand All @@ -98,6 +114,9 @@ function saveUserInfo() {
<NFormItem path="modelName" label="Model Name">
<NSelect v-model:value="model.modelName" placeholder="Select" :options="models" />
</NFormItem>
<NFormItem path="host" label="Host">
<NInput v-model:value="model.host" placeholder="" />
</NFormItem>
<NFormItem path="proxy" label="Proxy">
<NInput v-model:value="model.proxy" placeholder="socks5:https://127.0.0.1:7890" />
</NFormItem>
Expand Down
2 changes: 2 additions & 0 deletions src/store/modules/user/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface UserInfo {
export interface UserConfig {
modelName: string
apiKey: string
host: string
proxy: string | null
maxTokenNum: number
}
Expand All @@ -30,6 +31,7 @@ export function defaultSetting(): UserState {
modelName: 'gpt-3.5-turbo',
apiKey: import.meta.env.VITE_GLOB_OPENAI_KEY,
proxy: null,
host: 'https://api.openai.com/v1/chat/completions',
maxTokenNum: 4096,
},
}
Expand Down
1 change: 1 addition & 0 deletions src/views/chat/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function useChat() {

const defaultOpt = {
apiKey: userStore.userConfig.apiKey,
host: userStore.userConfig.host,
proxy: userStore.userConfig.proxy,
model: userStore.userConfig.modelName,
systemMessage: _getDefaultSystemMessage(),
Expand Down
7 changes: 5 additions & 2 deletions src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { storeToRefs } from 'pinia'
import { NAutoComplete, NButton, NInput, useDialog, useMessage } from 'naive-ui'
import { NAutoComplete, NButton, NInput, useDialog, useMessage, useNotification } from 'naive-ui'
import html2canvas from 'html2canvas'
import { invoke } from '@tauri-apps/api'
import { Message } from './components'
Expand All @@ -17,6 +17,7 @@ import { useChatStore, usePromptStore } from '@/store'
import { fetchChatAPIProcess } from '@/api'
import { t } from '@/locales'
const notification = useNotification()
let controller = new AbortController()
const route = useRoute()
Expand Down Expand Up @@ -85,7 +86,9 @@ async function fetchChatMessage(messages: Chat.RequestMessage[], uuid: number, i
)
}
else {
ms.error(errorMessage)
notification.error({
content: errorMessage,
})
updateChat(+uuid, index,
{
dateTime: new Date().toLocaleString(),
Expand Down

0 comments on commit af8ae9a

Please sign in to comment.