|
@@ -2,42 +2,53 @@ import axios from "@/services/config"
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
|
|
|
|
|
|
|
|
|
+let model = {}
|
|
|
|
|
+
|
|
|
interface ChatParams {
|
|
interface ChatParams {
|
|
|
|
|
+ id: string;
|
|
|
|
|
+ message: string;
|
|
|
|
|
+ userId: string;
|
|
|
model: string;
|
|
model: string;
|
|
|
|
|
+ file_ids: string[];
|
|
|
|
|
+ sound_url: string;
|
|
|
temperature: number;
|
|
temperature: number;
|
|
|
- max_tokens: number;
|
|
|
|
|
top_p: number;
|
|
top_p: number;
|
|
|
- frequency_penalty: number;
|
|
|
|
|
- presence_penalty: number;
|
|
|
|
|
- messages: Array<{ role: string; content: string }>;
|
|
|
|
|
- uid: string;
|
|
|
|
|
- mind_map_question: string;
|
|
|
|
|
|
|
+ max_completion_tokens: number;
|
|
|
stream: boolean;
|
|
stream: boolean;
|
|
|
- response_format: { type: string };
|
|
|
|
|
|
|
+ uid: string;
|
|
|
|
|
+ session_name: string;
|
|
|
|
|
+ tts_language: string;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const DEFAULT_PARAMS: Omit<ChatParams, 'messages' | 'uid'> = {
|
|
|
|
|
- model: "gpt-4o-2024-11-20",
|
|
|
|
|
- temperature: 0,
|
|
|
|
|
- max_tokens: 4096,
|
|
|
|
|
|
|
+const DEFAULT_PARAMS: Omit<ChatParams, 'message' | 'uid' | 'stream'> = {
|
|
|
|
|
+ id: "a7741704-ba56-40b7-a6b8-62a423ef9376",
|
|
|
|
|
+ userId: "6c56ec0e-2c74-11ef-bee5-005056b86db5",
|
|
|
|
|
+ model: "open-doubao",
|
|
|
|
|
+ file_ids: [],
|
|
|
|
|
+ sound_url: "",
|
|
|
|
|
+ temperature: 0.2,
|
|
|
top_p: 1,
|
|
top_p: 1,
|
|
|
- frequency_penalty: 0,
|
|
|
|
|
- presence_penalty: 0,
|
|
|
|
|
- mind_map_question: "",
|
|
|
|
|
- stream: false,
|
|
|
|
|
- response_format: { type: "text" }
|
|
|
|
|
-};
|
|
|
|
|
|
|
+ max_completion_tokens: 4096,
|
|
|
|
|
+ session_name: "pptSession_name",
|
|
|
|
|
+ tts_language: "zh-CN"
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-export const chat_no_stream = async (msg: string): Promise<string> => {
|
|
|
|
|
|
|
+export const chat_no_stream = async (msg: string, agentId: string, userId: string, language: string): Promise<string> => {
|
|
|
|
|
+ let modelType = await getAgentModel(agentId)
|
|
|
const params: ChatParams = {
|
|
const params: ChatParams = {
|
|
|
...DEFAULT_PARAMS,
|
|
...DEFAULT_PARAMS,
|
|
|
- messages: [{ role: "user", content: msg }],
|
|
|
|
|
|
|
+ id: agentId,
|
|
|
|
|
+ message: msg,
|
|
|
uid: uuidv4(),
|
|
uid: uuidv4(),
|
|
|
- stream: false
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ stream: false,
|
|
|
|
|
+ model: modelType,
|
|
|
|
|
+ userId: userId,
|
|
|
|
|
+ tts_language: language,
|
|
|
|
|
+ session_name:uuidv4()
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- const res = await axios.post('https://appapi.cocorobo.cn/api/common/chat', params);
|
|
|
|
|
|
|
+ const res = await axios.post('https://appapi.cocorobo.cn/api/agentchats/ai_agent_chat', params);
|
|
|
let content = res.FunctionResponse.choices[0].message.content;
|
|
let content = res.FunctionResponse.choices[0].message.content;
|
|
|
|
|
|
|
|
// 清理可能的 markdown 格式
|
|
// 清理可能的 markdown 格式
|
|
@@ -63,19 +74,28 @@ export const chat_no_stream = async (msg: string): Promise<string> => {
|
|
|
|
|
|
|
|
export const chat_stream = async (
|
|
export const chat_stream = async (
|
|
|
msg: string,
|
|
msg: string,
|
|
|
|
|
+ agentId: string,
|
|
|
|
|
+ userId: string,
|
|
|
|
|
+ language: string,
|
|
|
onMessage: (event: { type: 'message' | 'close' | 'error' | 'messageEnd'; data: string }) => void
|
|
onMessage: (event: { type: 'message' | 'close' | 'error' | 'messageEnd'; data: string }) => void
|
|
|
): Promise<void> => {
|
|
): Promise<void> => {
|
|
|
|
|
+ let modelType = await getAgentModel(agentId)
|
|
|
const params: ChatParams = {
|
|
const params: ChatParams = {
|
|
|
...DEFAULT_PARAMS,
|
|
...DEFAULT_PARAMS,
|
|
|
- messages: [{ role: "user", content: msg }],
|
|
|
|
|
|
|
+ id: agentId,
|
|
|
|
|
+ message: msg,
|
|
|
uid: uuidv4(),
|
|
uid: uuidv4(),
|
|
|
- stream: true
|
|
|
|
|
|
|
+ stream: true,
|
|
|
|
|
+ model: modelType,
|
|
|
|
|
+ userId: userId,
|
|
|
|
|
+ tts_language: language,
|
|
|
|
|
+ session_name:uuidv4()
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const ctrl = new AbortController();
|
|
const ctrl = new AbortController();
|
|
|
let content = ''
|
|
let content = ''
|
|
|
try {
|
|
try {
|
|
|
- await fetchEventSource('https://appapi.cocorobo.cn/api/common/chat', {
|
|
|
|
|
|
|
+ await fetchEventSource('https://appapi.cocorobo.cn/api/agentchats/ai_agent_chat', {
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
|
body: JSON.stringify(params),
|
|
body: JSON.stringify(params),
|
|
|
signal: ctrl.signal,
|
|
signal: ctrl.signal,
|
|
@@ -119,3 +139,12 @@ export const chat_stream = async (
|
|
|
ctrl.abort();
|
|
ctrl.abort();
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+export const getAgentModel = async (agentId: string) => {
|
|
|
|
|
+ if (model[agentId]) {
|
|
|
|
|
+ return model[agentId]
|
|
|
|
|
+ }
|
|
|
|
|
+ let res = await axios.get(`https://appapi.cocorobo.cn/api/agents/agent/${agentId}`)
|
|
|
|
|
+ model[agentId] = res['modelType']
|
|
|
|
|
+ return model[agentId]
|
|
|
|
|
+}
|