MCPcopy
hub / github.com/msgbyte/tianji / requestOpenAI

Function requestOpenAI

src/server/model/openai.ts:44–113  ·  view source on GitHub ↗
(
  workspaceId: string,
  prompt: string,
  question: string,
  options: RequestOpenAIOptions = {},
  context?: Record<string, string>
)

Source from the content-addressed store, hash-verified

42>;
43
44export async function requestOpenAI(
45 workspaceId: string,
46 prompt: string,
47 question: string,
48 options: RequestOpenAIOptions = {},
49 context?: Record<string, string>
50): Promise<string> {
51 if (!env.openai.enable) {
52 return '';
53 }
54
55 await checkCredit(workspaceId);
56
57 const messages: ChatCompletionMessageParam[] = [
58 {
59 role: 'system',
60 content: prompt,
61 },
62 {
63 role: 'user',
64 content: question,
65 },
66 ];
67
68 const res = await getOpenAIClient().chat.completions.create({
69 ...options,
70 model: modelName,
71 messages: messages,
72 });
73
74 const content = res.choices[0].message.content;
75 const usage = res.usage;
76
77 const credit = tokenCreditFactor * (usage?.total_tokens ?? 0);
78
79 if (env.debugAIFeature) {
80 console.log('[DEBUG AI] Start =======================');
81 console.log('[DEBUG AI] Prompt:');
82 for (const m of messages) {
83 console.log(m.role);
84 console.log(m.content);
85 }
86 console.log('[DEBUG AI] Prompt END');
87
88 console.log('[DEBUG AI] Output:');
89 console.log(content);
90 console.log('[DEBUG AI] Output END');
91
92 console.log('[DEBUG AI] Usage:');
93 console.log(usage);
94
95 console.log('[DEBUG AI] End =======================');
96 }
97
98 await costCredit(workspaceId, credit, 'ai', {
99 ...usage,
100 ...context,
101 });

Callers 2

Calls 5

checkCreditFunction · 0.85
getOpenAIClientFunction · 0.85
costCreditFunction · 0.85
createAuditLogFunction · 0.85
logMethod · 0.80

Tested by

no test coverage detected