Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance context function #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/utils/useChatAPI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { fetchApi } from '.'
import { chatyDebug } from '../main/prepare/debug'
const chatGPTUrl = 'https://api.openai.com/v1/chat/completions'
const chatWithGPT = async (messages: any[]) => {

const chatWithGPT = async (messages: any[], context: string) => {
const headers: Record<string, any> = {
Authorization: `Bearer ${process.env.OPEN_AI_KEY!}`
}
Expand All @@ -20,7 +21,8 @@ const chatWithGPT = async (messages: any[]) => {
{ headers },
{
model: 'gpt-3.5-turbo',
messages
messages,
context // 添加 context 参数到请求体中
}
)
return answer
Expand Down Expand Up @@ -77,17 +79,11 @@ export async function sendMessage (message: string, user: string) {
try {
messageManager.sendMessage(message, user)
const messages = messageManager.getMessages(user)
// chatyDebug("-----------newMessages----------");
// chatyDebug(messages);
// chatyDebug("-----------newMessages----------");
const completion = await chatWithGPT(messages!)
const context = JSON.stringify(messages) // 将聊天历史记录序列化为上下文信息
const completion = await chatWithGPT(messages!, context) // 传递上下文信息到 chatWithGPT 函数中
const answer = completion.choices[0].message.content

// chatyDebug("-----------newAnswers----------");
// chatyDebug(answer);
// chatyDebug("-----------newAnswers----------");
messageManager.concatAnswer(answer, user)
messageManager.addUsage(completion.usage, user);
messageManager.addUsage(completion.usage, user)
return answer
} catch (err) {
messageManager.popMessage(user)
Expand Down