Skip to content

Commit

Permalink
feat: 支持选择model
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghonglu.neo committed Mar 20, 2023
1 parent b29c4de commit b2919c2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
"dependencies": {
"@tauri-apps/api": "^1.2.0",
"@traptitech/markdown-it-katex": "^3.6.0",
"@types/lodash": "^4.14.191",
"@vueuse/core": "^9.13.0",
"highlight.js": "^11.7.0",
"html2canvas": "^1.4.1",
"katex": "^0.16.4",
"lodash": "^4.17.21",
"markdown-it": "^13.0.1",
"naive-ui": "^2.34.3",
"pinia": "^2.0.32",
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src-tauri/src/app/cmd/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ impl ProgressPayload {
pub async fn fetch_chat_api(
handle: AppHandle,
id: u64,
proxy: Option<String>,
token: String,
model: String,
messages: Vec<HashMap<String, String>>,
temperature: f32
) -> 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 model = "gpt-3.5-turbo";
let data = json!({
"model": model,
"messages": messages,
Expand Down
33 changes: 17 additions & 16 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,23 @@ fn main() {
cmd::gpt::fetch_chat_api,
cmd::download::download_img
])
.setup(builder::setup)
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|app, event| match event {
tauri::RunEvent::WindowEvent {
label,
event: win_event,
..
} => match win_event {
tauri::WindowEvent::CloseRequested { api, .. } => {
let win = app.get_window(label.as_str()).unwrap();
win.minimize().unwrap();
.setup(builder::setup);

#[cfg(target_os = "macos")]
{
builder = builder.on_window_event(move |event| {
if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() {
let win = event.window().clone();
if win.label() == "core" {
win.minimize().unwrap();
}else {
event.window().close().unwrap();
}
api.prevent_close();
}
_ => {}
},
_ => {}
});
})
}

builder.run(tauri::generate_context!())
.expect("error while running tauri application");
}
5 changes: 4 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ async function listenToEventIfNeeded(): Promise<void> {

export async function fetchChatAPIProcess(
apiKey: string,
proxy: string | null,
modelName: string,
messages: Chat.RequestMessage[],
progressHandler?: (detail: string, role: string) => void,
signal?: GenericAbortSignal,
Expand All @@ -55,10 +57,11 @@ export async function fetchChatAPIProcess(
handlers.delete(id)
}
}

await invoke('fetch_chat_api', {
id,
proxy,
token: apiKey,
model: modelName,
messages,
temperature: 0.6,
})
Expand Down
30 changes: 29 additions & 1 deletion src/components/common/Setting/User.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang='ts'>
import { computed, ref } from 'vue'
import type { FormInst, FormItemRule, FormRules } from 'naive-ui'
import { NButton, NForm, NFormItem, NInput, useMessage } from 'naive-ui'
import { NButton, NForm, NFormItem, NInput, NSelect, useMessage } from 'naive-ui'
import { useUserStore } from '@/store'
import { t } from '@/locales'
Expand All @@ -14,8 +14,15 @@ const model = ref({
name: userInfo.value.name,
avatar: userInfo.value.avatar,
apiKey: userInfo.value.apiKey,
modelName: userInfo.value.modelName,
proxy: userInfo.value.proxy,
})
const models = userStore.allModels().map(v => ({
label: v,
value: v,
}))
const rules: FormRules = {
name: [
{
Expand All @@ -30,6 +37,18 @@ const rules: FormRules = {
trigger: ['input', 'blur'],
},
],
proxy: [{
required: false,
validator(rule: FormItemRule, value: string) {
if (!value || value.length === 0)
return true
else if (!/^(socks5|http|https):\/\/.+$/.test(value))
return new Error('请输入正确的proxy')
return true
},
trigger: ['input', 'blur'],
}],
apiKey: [
{
required: true,
Expand All @@ -53,6 +72,9 @@ function saveUserInfo() {
userInfo.value.name = model.value.name
userInfo.value.avatar = model.value.avatar
userInfo.value.apiKey = model.value.apiKey
userInfo.value.modelName = model.value.modelName
userInfo.value.proxy = model.value.proxy
userStore.recordState()
ms.success(t('common.success'))
// window.location.reload()
Expand All @@ -73,6 +95,12 @@ function saveUserInfo() {
<NFormItem path="apiKey" label="Openai API Key">
<NInput v-model:value="model.apiKey" placeholder="Openai API Key" />
</NFormItem>
<NFormItem path="modelName" label="Model Name">
<NSelect v-model:value="model.modelName" placeholder="Select" :options="models" />
</NFormItem>
<!-- <NFormItem path="proxy" label="Proxy">
<NInput v-model:value="model.proxy" placeholder="http:https://127.0.0.1:7890" />
</NFormItem> -->
<div class="flex items-center justify-end">
<NButton size="small" @click="saveUserInfo">
{{ $t('setting.saveUserInfoBtn') }}
Expand Down
13 changes: 12 additions & 1 deletion src/store/modules/user/helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as _ from 'lodash'
import { ss } from '@/utils/storage'

const LOCAL_NAME = 'userStorage'

export interface UserInfo {
avatar: string
name: string | null
modelName: string
apiKey: string
proxy: string | null
}

export interface UserState {
Expand All @@ -17,14 +20,22 @@ export function defaultSetting(): UserState {
userInfo: {
avatar: '',
name: null,
modelName: 'gpt-3.5-turbo',
apiKey: import.meta.env.VITE_GLOB_OPENAI_KEY,
proxy: null,
},
}
}

export function allModels(): string[] {
return ['gpt-3.5-turbo', 'gpt-3.5-turbo-0301', 'gpt-4', 'gpt-4-0314', 'gpt-4-32k', 'gpt-4-32k-0314']
}

export function getLocalState(): UserState {
const localSetting: UserState | undefined = ss.get(LOCAL_NAME)
return { ...defaultSetting(), ...localSetting }
const ds = defaultSetting()
const state = _.merge(ds, localSetting)
return state
}

export function setLocalState(setting: UserState): void {
Expand Down
6 changes: 5 additions & 1 deletion src/store/modules/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia'
import type { UserInfo, UserState } from './helper'
import { defaultSetting, getLocalState, setLocalState } from './helper'
import { allModels, defaultSetting, getLocalState, setLocalState } from './helper'

export const useUserStore = defineStore('user-store', {
state: (): UserState => getLocalState(),
Expand All @@ -18,5 +18,9 @@ export const useUserStore = defineStore('user-store', {
recordState() {
setLocalState(this.$state)
},

allModels(): string[] {
return allModels()
},
},
})
4 changes: 3 additions & 1 deletion src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async function onConversation() {
try {
let lastText = ''
const fetchChatAPIOnce = async () => {
await fetchChatAPIProcess(api_key, messages, (detail: string, _: string) => {
await fetchChatAPIProcess(api_key, userInfo.value.proxy, userInfo.value.modelName, messages, (detail: string, _: string) => {
try {
lastText = lastText + detail ?? ''
updateChat(
Expand Down Expand Up @@ -218,6 +218,8 @@ async function onRegenerate(index: number) {
const fetchChatAPIOnce = async () => {
await fetchChatAPIProcess(
api_key,
userInfo.value.proxy,
userInfo.value.modelName,
messages!,
(detail: string, _: string) => {
try {
Expand Down

0 comments on commit b2919c2

Please sign in to comment.