Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Caojiahao-Coder committed Feb 21, 2024
2 parents 43fb890 + 4fb7236 commit ebac3bc
Show file tree
Hide file tree
Showing 20 changed files with 494 additions and 125 deletions.
17 changes: 16 additions & 1 deletion src/assets/models-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,19 @@ const gpt_models: {
},
]

export default gpt_models
const groq_models: {
value: string
name: string
}[] = [{
value: 'llama2-70b-4096',
name: 'LLaMA2-70b-chat',
},
{
value: 'mixtral-8x7b-32768',
name: 'Mixtral-8x7b-Instruct-v0.1',
}]

export {
groq_models,
gpt_models,
}
55 changes: 29 additions & 26 deletions src/chat.completion/ChatCompletionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import messageController from './MessageController'
import conversationController from './ConversationController'
import type { NewMessageInfo, TBConverstationInfo, TBMessageInfo, TBPromptInfo } from '@/database/table-type'
import type { ChatCompletionMessage } from '@/openai/type/chat.completion.message'
import openAIServices from '@/openai/logic/services'
import { handleChatCompletionPrecondition } from '@/openai/handler/ChatHandler'
import openAIServices from '@/openai/logic/services'
import ChatCompletionParser from '@/openai/parser/ChatCompletionParser'
import ChatCompletionParserByGroq from '@/groq/parser/ChatCompletionParser'
import useEditorStore from '@/store/editor-store'
import handleChatVisionPrecondition from '@/openai/handler/VisionHandler'
import type { ToolCallsInfo } from '@/openai/type/chat.completion.tool.calls'
import chatFunctionCallingController from '@/chat.function.calling/ChatFunctionCallingController'
import groqServices from '@/groq/logic/service'

/**
* 当前的对话处理器
Expand Down Expand Up @@ -77,30 +79,17 @@ class ChatCompletionHandler {
}
}

/**
* 获取某个对话的结果
* @param messageId
* @param resultCallback
* @returns
*/
async getMessageAnswerAsync(messageId: number, resultCallback: (value: string) => void, functionCallingResultCallback: (
tool_call_id: string,
functionName: string,
args: string,
isDone: boolean
) => void): Promise<boolean> {
async getMessageAnswerByGroqAsync(messageId: number, resultCallback: (value: string) => void) {
this.editorStore.thinking = true

this.lastMessageId = messageId

const messageInfo = await messageController.getMessageInfoByIdAsync(messageId)

const useVisionAPI = messageInfo.vision_file !== undefined

const messageList = await this.generateMessageListByMessageInfoAsync(messageInfo)

// 得到请求包
const chatCompletionResponse = await openAIServices.createChatCompletionsRequest(messageList, useVisionAPI)
const chatCompletionResponse = await groqServices.createChatCompletionsRequest(messageList)

if (chatCompletionResponse.code !== 1) {
this.editorStore.thinking = false
Expand All @@ -112,17 +101,14 @@ class ChatCompletionHandler {

let markResult = false

let needFunctionResult = false
const needFunctionResult = false

if (chatCompletionResponse.data !== null) {
let gptContent = ''

parserResult = await ChatCompletionParser(chatCompletionResponse.data!, (text) => {
parserResult = await ChatCompletionParserByGroq(chatCompletionResponse.data!, (text) => {
gptContent += text
resultCallback(text)
}, (tool_call_id, fname, args, isDone) => {
needFunctionResult = true
functionCallingResultCallback(tool_call_id, fname, args, isDone)
})

if (parserResult)
Expand All @@ -134,16 +120,31 @@ class ChatCompletionHandler {
return (parserResult && markResult)
}

async getDataWorkAnswerAsync(messageId: number, messageList: ChatCompletionMessage[], resultCallback: (value: string) => void) {
/**
* 获取某个对话的结果
* @param messageId
* @param resultCallback
* @returns
*/
async getMessageAnswerAsync(messageId: number, resultCallback: (value: string) => void, functionCallingResultCallback: (
tool_call_id: string,
functionName: string,
args: string,
isDone: boolean
) => void): Promise<boolean> {
this.editorStore.thinking = true

this.lastMessageId = messageId

// 得到请求包
const chatCompletionResponse = await openAIServices.createDataWorkRequest(messageList)

const messageInfo = await messageController.getMessageInfoByIdAsync(messageId)

const useVisionAPI = messageInfo.vision_file !== undefined

const messageList = await this.generateMessageListByMessageInfoAsync(messageInfo)

// 得到请求包
const chatCompletionResponse = await openAIServices.createChatCompletionsRequest(messageList, useVisionAPI)

if (chatCompletionResponse.code !== 1) {
this.editorStore.thinking = false
await this.markMessageErrorAsync(messageInfo, chatCompletionResponse.message)
Expand All @@ -162,8 +163,9 @@ class ChatCompletionHandler {
parserResult = await ChatCompletionParser(chatCompletionResponse.data!, (text) => {
gptContent += text
resultCallback(text)
}, () => {
}, (tool_call_id, fname, args, isDone) => {
needFunctionResult = true
functionCallingResultCallback(tool_call_id, fname, args, isDone)
})

if (parserResult)
Expand Down Expand Up @@ -225,6 +227,7 @@ class ChatCompletionHandler {
conversation_token: conversationInfo.conversation_token,
fixed_top: conversationInfo.fixed_top,
type: conversationInfo.type,
use_groq: conversationInfo.use_groq,
} as TBConverstationInfo

conversationController.updateConversationInfoAsync(newInfo)
Expand Down
3 changes: 3 additions & 0 deletions src/chat.completion/ConversationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class ConversationController {
description: '',
conversation_token: uid(32),
type: 'chat',
use_groq: false,
} as NewConverstationInfo

return await this.addConversationAsync(newInfo)
Expand All @@ -110,6 +111,7 @@ class ConversationController {
description: '',
conversation_token: uid(32),
type: 'dataworker',
use_groq: false,
} as NewConverstationInfo

return await this.addConversationAsync(newInfo)
Expand All @@ -126,6 +128,7 @@ class ConversationController {
description: '',
conversation_token: uid(32),
type: 'draw_img_mode',
use_groq: false,
} as NewConverstationInfo

return await this.addConversationAsync(newInfo)
Expand Down
56 changes: 53 additions & 3 deletions src/components/ConversationBody.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import useConversationStore from '@/store/conversation-store'
import conversationController from '@/chat.completion/ConversationController'
import type { TBConverstationInfo, TBMessageInfo } from '@/database/table-type'
import useChatCompletionStore from '@/store/chat-completion-store'
import { groqApiKey, groqBaseURL, groqModel } from '@/store/localstorage'
import { push } from '@/main'
const { t } = useI18n()
Expand Down Expand Up @@ -50,22 +52,54 @@ function onCreateDrawImageConversation() {
color: 'bg-yellow-2',
fixed_top: info.fixed_top ?? false,
type: 'draw_img_mode',
}
useGroqAPI: info.use_groq,
} as TBConverstationInfo
updateConversationInfo(newInfo as TBConverstationInfo)
}
function updateConversationInfo(newInfo: TBConverstationInfo) {
conversationController.updateConversationInfoAsync(newInfo)
}
function onUseGroqAPI() {
const info = chatCompletionStore.chatCompletionHandler?.getConversationInfo()
if (info?.use_groq === false) {
if (groqApiKey.value.length === 0 || groqBaseURL.value.length === 0 || groqModel.value.length === 0) {
push.warning(t('use_groq_api.cannot_empty'))
return
}
}
updateConversationUseAPI(!info.use_groq)
}
function updateConversationUseAPI(useGroqAPI: boolean) {
const conversationStore = useConversationStore()
const info = conversationStore.conversationInfo!
const newInfo = {
id: info.id,
title: 'Chat Completion (Grop API)',
create_time: info.create_time,
description: info.description,
conversation_token: info.conversation_token,
color: 'bg-blue',
fixed_top: info.fixed_top ?? false,
type: 'chat',
use_groq: useGroqAPI,
} as TBConverstationInfo
updateConversationInfo(newInfo as TBConverstationInfo)
}
</script>

<template>
<div ref="bodyRef" class="relative h-100% records-list color-base bg-body" overflow="x-hidden y-scroll">
<div id="conversation-body">
<MessageRecordItem
v-for="(item, index) in myMessageList" :key="index" :message-info="item"
:message-index="index"
v-for="(item, index) in myMessageList" :key="index" :message-info="item" :message-index="index"
:scroll-body="updateScroll" @on-reload-message-list="() => loadMessageList()"
/>
</div>
Expand Down Expand Up @@ -111,6 +145,22 @@ function updateConversationInfo(newInfo: TBConverstationInfo) {
</div>
<div class="flex-1" />
</div>

<div
data-cursor="block"
class="border select-none bg-base shadow-2xl h-48px flex flex-row color-base gap-2 cursor-pointer p-x-4 flex flex-col hover-bg-body"
@click="onUseGroqAPI"
>
<div class="flex-1" />
<div class="flex flex-row gap-2">
<input type="checkbox" :checked="chatCompletionStore.chatCompletionHandler?.getConversationInfo().use_groq ?? false" class="w-22px h-22px m-0">
<div class="w-22px h-22px i-carbon-face-wink-filled" />
<div class="line-height-22px">
{{ t('use_groq_api.title') }}
</div>
</div>
<div class="flex-1" />
</div>
</div>
</div>
</template>
Expand Down
3 changes: 2 additions & 1 deletion src/components/ConversationItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ function fixedTop() {
color: info.color,
fixed_top: !(info.fixed_top ?? false),
type: info.type,
}
use_groq: info.use_groq,
} as TBConverstationInfo
conversationController.updateConversationInfoAsync(newConversationInfo).then((res) => {
if (res)
Expand Down
4 changes: 3 additions & 1 deletion src/components/EditSessionSettingsDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useConversationStore from '@/store/conversation-store'
import SelectColorDialog from '@/ui/SelectColorDialog.vue'
import { push } from '@/main'
import conversationController from '@/chat.completion/ConversationController'
import type { TBConverstationInfo } from '@/database/table-type'
const { t } = useI18n()
Expand Down Expand Up @@ -34,7 +35,8 @@ async function onSaveSessionSettings() {
color: color.value,
fixed_top: info.fixed_top ?? false,
type: info.type,
}
use_groq: info.use_groq,
} as TBConverstationInfo
const result = await conversationController.updateConversationInfoAsync(newConversationInfo)
if (result) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/FunctionCallingDetailView.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type { ToolInfo } from '@/openai/tool-call'
import type { FunctionInfo } from '@/openai/type/chat.completion.function.calling'
import Markdown from '@/components/Markdown.vue'
const props = defineProps<{
functionCallingInfo: ToolInfo | undefined
functionCallingInfo: FunctionInfo | undefined
}>()
const { t } = useI18n()
Expand Down

0 comments on commit ebac3bc

Please sign in to comment.