MCPcopy
hub / github.com/ChatGPTNextWeb/NextChat / chat

Method chat

app/client/platforms/xai.ts:63–183  ·  view source on GitHub ↗
(options: ChatOptions)

Source from the content-addressed store, hash-verified

61 }
62
63 async chat(options: ChatOptions) {
64 const messages: ChatOptions["messages"] = [];
65 for (const v of options.messages) {
66 const content = await preProcessImageContent(v.content);
67 messages.push({ role: v.role, content });
68 }
69
70 const modelConfig = {
71 ...useAppConfig.getState().modelConfig,
72 ...useChatStore.getState().currentSession().mask.modelConfig,
73 ...{
74 model: options.config.model,
75 providerName: options.config.providerName,
76 },
77 };
78
79 const requestPayload: RequestPayload = {
80 messages,
81 stream: options.config.stream,
82 model: modelConfig.model,
83 temperature: modelConfig.temperature,
84 presence_penalty: modelConfig.presence_penalty,
85 frequency_penalty: modelConfig.frequency_penalty,
86 top_p: modelConfig.top_p,
87 };
88
89 console.log("[Request] xai payload: ", requestPayload);
90
91 const shouldStream = !!options.config.stream;
92 const controller = new AbortController();
93 options.onController?.(controller);
94
95 try {
96 const chatPath = this.path(XAI.ChatPath);
97 const chatPayload = {
98 method: "POST",
99 body: JSON.stringify(requestPayload),
100 signal: controller.signal,
101 headers: getHeaders(),
102 };
103
104 // make a fetch request
105 const requestTimeoutId = setTimeout(
106 () => controller.abort(),
107 getTimeoutMSByModel(options.config.model),
108 );
109
110 if (shouldStream) {
111 const [tools, funcs] = usePluginStore
112 .getState()
113 .getAsTools(
114 useChatStore.getState().currentSession().mask?.plugin || [],
115 );
116 return stream(
117 chatPath,
118 requestPayload,
119 getHeaders(),
120 tools as any,

Callers 2

onUserInputFunction · 0.45
summarizeSessionFunction · 0.45

Calls 7

pathMethod · 0.95
extractMessageMethod · 0.95
preProcessImageContentFunction · 0.90
getHeadersFunction · 0.90
getTimeoutMSByModelFunction · 0.90
streamFunction · 0.90
fetchFunction · 0.90

Tested by

no test coverage detected