Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: latency calculation for claude, gemini, gpt chat #216

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/short-clouds-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@empiricalrun/ai": patch
---

fix: latency calculation for claude, gemini, gpt chat
3 changes: 2 additions & 1 deletion packages/ai/src/providers/anthropic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
const { contents, systemPrompt } = convertOpenAIToAnthropicAI(messages);
const { executionDone } = await batchTaskManager.waitForTurn();
try {
const startedAt = Date.now();
let startedAt = Date.now();
const response = await promiseRetry<Anthropic.Beta.Tools.ToolsBetaMessage>(
(retry, attempt) => {
return anthropic.beta.tools.messages
Expand Down Expand Up @@ -122,6 +122,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
console.warn(
`Retrying request for anthropic model: ${body.model}. Retry attempt: ${attempt}`,
);
startedAt = Date.now();
retry(err);
throw err;
}
Expand Down
11 changes: 4 additions & 7 deletions packages/ai/src/providers/google/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ describe.concurrent("test google ai provider", () => {
messages: [
{
role: "user",
content: `List a few popular cookie recipes using this JSON schema: {'type': 'object', 'properties': { 'recipe_name': {'type': 'string'}}}`,
content: `List a few popular cookie recipes using this JSON schema: {'type': 'array', 'items': { 'type': 'object', 'properties': { 'recipe_name': { 'type': 'string' }}}}`,
},
],
response_mime_type: "application/json",
});
expect(chatCompletion.choices.length).toBeGreaterThan(0);
expect(
JSON.parse(chatCompletion.choices[0]?.message.content!),
).toBeDefined();
expect(
JSON.parse(chatCompletion.choices[0]?.message.content!)[0]["recipe_name"],
).toBeDefined();
const contents = chatCompletion.choices[0]?.message.content!;
expect(JSON.parse(contents)).toBeDefined();
expect(JSON.parse(contents)[0]["recipe_name"]).toBeDefined();
}, 120000);

test("passthrough model parameters works", async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/ai/src/providers/google/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
};
const { executionDone } = await batch.waitForTurn();
try {
const startedAt = Date.now();
let startedAt = Date.now();
const completion = await promiseRetry<GenerateContentResult>(
(retry, attempt) => {
return modelInstance
Expand All @@ -170,6 +170,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
console.warn(
`Retrying request for google model: ${model}. Retry attempt: ${attempt}`,
);
startedAt = Date.now();
retry(err);
}
throw err;
Expand Down
3 changes: 2 additions & 1 deletion packages/ai/src/providers/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
});

try {
const startedAt = Date.now();
let startedAt = Date.now();
const completions = await promiseRetry<IChatCompletion>(
(retry, attempt) => {
return openai.chat.completions.create(body).catch((err) => {
Expand All @@ -49,6 +49,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
console.warn(
`Retrying request for openai model: ${body.model}. Retry attempt: ${attempt}`,
);
startedAt = Date.now();
retry(err);
throw err;
}
Expand Down