Skip to content

Commit f42e440

Browse files
committed
safer
1 parent f49bf04 commit f42e440

File tree

8 files changed

+57
-42
lines changed

8 files changed

+57
-42
lines changed

e2e/lib/ai-gateway/completions.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ import OpenAI from 'openai';
22

33
const TOKEN_REPEAT_COUNT = 2048;
44

5-
const calculatorTool = {
5+
const weatherTool = {
66
type: "function",
77
function: {
8-
name: "calculate",
9-
description: "Calculate a mathematical expression",
8+
name: "get_current_weather",
9+
description: "Get the current weather in a given location",
1010
parameters: {
1111
type: "object",
1212
properties: {
13-
expression: {
13+
location: {
1414
type: "string",
15-
description: "The mathematical expression to calculate"
15+
description: "The city and state, e.g. San Francisco, CA"
16+
},
17+
unit: {
18+
type: "string",
19+
enum: ["celsius", "fahrenheit"]
1620
}
1721
},
18-
required: ["expression"]
22+
required: ["location", "unit"]
1923
}
2024
}
2125
};
@@ -121,14 +125,6 @@ function processNonStreamingResponse(completion: any): { complete: any } {
121125
return { complete: completion.choices[0].message };
122126
}
123127

124-
function executeCalculator(expression: string): number {
125-
try {
126-
return eval(expression);
127-
} catch (error) {
128-
throw new Error(`Failed to calculate expression: ${expression}`);
129-
}
130-
}
131-
132128
export async function CreateChatCompletion(params: {
133129
client: OpenAI,
134130
model: string,
@@ -148,10 +144,12 @@ export async function CreateChatCompletion(params: {
148144

149145
const responses: Array<{ stream?: any[], complete: any }> = [];
150146

151-
const baseMessage = useTools ? "What is 50+50? Use the tool." : "What is 50+50?";
147+
const baseMessage = useTools
148+
? "What is the weather in San Francisco in Celsius? Tell me your plan, then use the tool."
149+
: "What is the weather in San Francisco in Celsius?";
152150
const userMessage = baseMessage;
153151
const messages = [{ role: "user", content: userMessage }];
154-
const tools = useTools ? [calculatorTool, searchWebTool] : undefined;
152+
const tools = useTools ? [weatherTool, searchWebTool] : undefined;
155153

156154
const completion = await makeRequest(client, model, stream, testProviderCache, cacheTriggerToken, messages, tools);
157155

@@ -163,16 +161,15 @@ export async function CreateChatCompletion(params: {
163161

164162
if (useTools && firstResponse.complete.tool_calls && firstResponse.complete.tool_calls.length > 0) {
165163
const toolCall = firstResponse.complete.tool_calls[0];
166-
if (toolCall.function.name === "calculate") {
167-
const expression = JSON.parse(toolCall.function.arguments).expression;
168-
const result = executeCalculator(expression);
164+
if (toolCall.function.name === "get_current_weather") {
165+
const result = "It is sunny at 30 degrees Celsius in San Francisco.";
169166

170167
const finalMessages = [
171168
...messages,
172169
firstResponse.complete,
173170
{
174171
role: "tool",
175-
content: result.toString(),
172+
content: result,
176173
tool_call_id: toolCall.id
177174
}
178175
];

packages/cost/models/providers/base.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export abstract class BaseProvider {
7171
}> {
7272
try {
7373
const respJson = (await response.json()) as any;
74-
console.log('respJson', JSON.stringify(respJson, null, 2));
7574
if (respJson.error?.message) {
7675
return { message: respJson.error.message };
7776
}

packages/cost/models/providers/bedrock.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,14 @@ export class BedrockProvider extends BaseProvider {
140140
message: string;
141141
details?: any;
142142
}> {
143-
const respJson = (await response.json()) as any;
144-
if (respJson.message) {
145-
return { message: respJson.message, details: respJson };
143+
try {
144+
const respJson = (await response.json()) as any;
145+
if (respJson.message) {
146+
return { message: respJson.message, details: respJson };
147+
}
148+
return { message: `Failed request with status ${response.status}` };
149+
} catch (error) {
150+
return { message: `Request failed with status ${response.status}` };
146151
}
147-
return { message: `Failed request with status ${response.status}` };
148152
}
149153
}

packages/cost/models/providers/chutes.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ export class ChutesProvider extends BaseProvider {
1515
message: string;
1616
details?: any;
1717
}> {
18-
const respJson = (await response.json()) as any;
19-
return {
20-
message: respJson.detail.message || `Request failed with status ${response.status}`
21-
};
18+
try {
19+
const respJson = (await response.json()) as any;
20+
return {
21+
message: respJson.detail?.message || `Request failed with status ${response.status}`
22+
};
23+
} catch (error) {
24+
return { message: `Request failed with status ${response.status}` };
25+
}
2226
}
2327
}

packages/cost/models/providers/mistral.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export class MistralProvider extends BaseProvider {
3030
}> {
3131
try {
3232
const respJson = (await response.json()) as any;
33-
console.log('respJson', JSON.stringify(respJson, null, 2));
3433
return {
3534
message: respJson.message?.detail?.[0]?.msg || respJson.detail?.[0]?.msg || `Request failed with status ${response.status}`,
3635
details: respJson.message?.detail || respJson.detail

packages/cost/models/providers/nebius.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ export class NebiusProvider extends BaseProvider {
1919
message: string;
2020
details?: any;
2121
}> {
22-
const respJson = (await response.json()) as any;
23-
return {
24-
message: respJson.detail || `Request failed with status ${response.status}`
25-
};
22+
try {
23+
const respJson = (await response.json()) as any;
24+
return {
25+
message: respJson.detail || `Request failed with status ${response.status}`
26+
};
27+
} catch (error) {
28+
return { message: `Request failed with status ${response.status}` };
29+
}
2630
}
2731
}

packages/cost/models/providers/novita.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ export class NovitaProvider extends BaseProvider {
1616
message: string;
1717
details?: any;
1818
}> {
19-
const respJson = (await response.json()) as any;
20-
return {
21-
message: respJson.message || `Request failed with status ${response.status}`
22-
};
19+
try {
20+
const respJson = (await response.json()) as any;
21+
return {
22+
message: respJson.message || `Request failed with status ${response.status}`
23+
};
24+
} catch (error) {
25+
return { message: `Request failed with status ${response.status}` };
26+
}
2327
}
2428
}

packages/cost/models/providers/perplexity.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ export class PerplexityProvider extends BaseProvider {
1616
message: string;
1717
details?: any;
1818
}> {
19-
const respJson = (await response.json()) as any;
20-
return {
21-
message: respJson.error.message || `Request failed with status ${response.status}`
22-
};
19+
try {
20+
const respJson = (await response.json()) as any;
21+
return {
22+
message: respJson.error?.message || `Request failed with status ${response.status}`
23+
};
24+
} catch (error) {
25+
return { message: `Request failed with status ${response.status}` };
26+
}
2327
}
2428
}

0 commit comments

Comments
 (0)