File size: 1,259 Bytes
2ab3016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ModelConfig } from "../config/modelConfig";

export async function sendToModel(
  config: ModelConfig,
  systemPrompt: string,
  projectRules: string,
  memoryContent: string,
  userPrompt: string
): Promise<string> {
  try {
    const url = config.baseUrl + "/v1/chat/completions";
    const response = await fetch(url, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        model: config.modelName,
        temperature: config.temperature,
        top_p: config.top_p,
        max_tokens: config.max_tokens,
        messages: [
          { role: "system", content: systemPrompt },
          { role: "user", content: projectRules + "\n\n" + memoryContent + "\n\n" + userPrompt }
        ]
      })
    });

    if (!response.ok) {
      throw new Error("Model API error: " + response.statusText);
    }

    const data = await response.json();
    if (!data.choices || data.choices.length === 0 || !data.choices[0].message || typeof data.choices[0].message.content !== "string") {
      throw new Error("Invalid model API response");
    }

    return data.choices[0].message.content;
  } catch (error) {
    console.error("Error sending request to model:", error);
    throw error;
  }
}