-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
255 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { calculateOpenAiCallCostInMillicent } from "../provider/openai/calculateOpenAiCallCostInMillicent"; | ||
import { Run } from "./Run"; | ||
|
||
export const calculateRunCostInMillicent = async ({ run }: { run: Run }) => { | ||
const callCostsInMillicent = run.recordedCalls.map((call) => { | ||
if (call.success && call.metadata.model.vendor === "openai") { | ||
return calculateOpenAiCallCostInMillicent(call); | ||
} | ||
return undefined; | ||
}); | ||
|
||
return callCostsInMillicent.reduce( | ||
(sum: number, cost) => sum + (cost ?? 0), | ||
0 | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from "./GenerateCall"; | ||
export * from "./Run"; | ||
export * from "./RunContext"; | ||
export * from "./calculateRunCostInMillicent"; | ||
export * as controller from "./controller/index"; | ||
export * as observer from "./observer/index"; | ||
export * from "./runAgent"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { RunObserver } from "./RunObserver"; | ||
|
||
export const combineObservers = (...observers: RunObserver[]): RunObserver => ({ | ||
onRunStarted: ({ run }) => { | ||
observers.forEach((observer) => observer.onRunStarted?.({ run })); | ||
}, | ||
|
||
onRunFinished: ({ run, result }) => { | ||
observers.forEach((observer) => observer.onRunFinished?.({ run, result })); | ||
}, | ||
|
||
onStepGenerationStarted: ({ run }) => { | ||
observers.forEach((observer) => | ||
observer.onStepGenerationStarted?.({ run }) | ||
); | ||
}, | ||
|
||
onStepGenerationFinished: ({ run, generatedText, step }) => { | ||
observers.forEach((observer) => | ||
observer.onStepGenerationFinished?.({ run, generatedText, step }) | ||
); | ||
}, | ||
|
||
onLoopIterationStarted: ({ run, loop }) => { | ||
observers.forEach((observer) => | ||
observer.onLoopIterationStarted?.({ run, loop }) | ||
); | ||
}, | ||
|
||
onLoopIterationFinished: ({ run, loop }) => { | ||
observers.forEach((observer) => | ||
observer.onLoopIterationFinished?.({ run, loop }) | ||
); | ||
}, | ||
|
||
onStepExecutionStarted: ({ run, step }) => { | ||
observers.forEach((observer) => | ||
observer.onStepExecutionStarted?.({ run, step }) | ||
); | ||
}, | ||
|
||
onStepExecutionFinished: ({ run, step, result }) => { | ||
observers.forEach((observer) => | ||
observer.onStepExecutionFinished?.({ run, step, result }) | ||
); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./RunObserver"; | ||
export * from "./combineObservers"; | ||
export * from "./showRunInConsole"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./generateChatCompletion.js"; | ||
export * from "./generateCompletion.js"; | ||
export * from "./generateTextCompletion.js"; |
28 changes: 0 additions & 28 deletions
28
packages/agent/src/provider/openai/calculateCallCostInMillicent.ts
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
packages/agent/src/provider/openai/calculateChatCompletionCostInMillicent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
OpenAIChatCompletion, | ||
OpenAIChatCompletionModel, | ||
} from "./api/generateChatCompletion"; | ||
|
||
// see https://openai.com/pricing | ||
const promptTokenCostInMillicent = { | ||
"gpt-4": 3, | ||
"gpt-3.5-turbo": 0.2, | ||
}; | ||
|
||
const completionTokenCostInMillicent = { | ||
"gpt-4": 6, | ||
"gpt-3.5-turbo": 0.2, | ||
}; | ||
|
||
export const calculateChatCompletionCostInMillicent = ({ | ||
model, | ||
completion, | ||
}: { | ||
model: OpenAIChatCompletionModel; | ||
completion: OpenAIChatCompletion; | ||
}) => | ||
completion.usage.prompt_tokens * promptTokenCostInMillicent[model] + | ||
completion.usage.completion_tokens * completionTokenCostInMillicent[model]; |
49 changes: 49 additions & 0 deletions
49
packages/agent/src/provider/openai/calculateOpenAiCallCostInMillicent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { GenerateCall } from "../../agent"; | ||
import { OpenAIChatCompletionSchema } from "./api/generateChatCompletion"; | ||
import { OpenAITextCompletionSchema } from "./api/generateTextCompletion"; | ||
import { calculateChatCompletionCostInMillicent } from "./calculateChatCompletionCostInMillicent"; | ||
import { calculateTextCompletionCostInMillicent } from "./calculateTextCompletionCostInMillicent"; | ||
|
||
export function calculateOpenAiCallCostInMillicent( | ||
call: GenerateCall & { | ||
success: true; | ||
} | ||
) { | ||
if (call.metadata.model.vendor !== "openai") { | ||
throw new Error(`Incorrect vendor: ${call.metadata.model.vendor}`); | ||
} | ||
|
||
const model = call.metadata.model.name; | ||
|
||
switch (model) { | ||
case "gpt-3.5-turbo": | ||
case "gpt-4": { | ||
return calculateChatCompletionCostInMillicent({ | ||
model, | ||
completion: OpenAIChatCompletionSchema.parse(call.rawOutput), | ||
}); | ||
} | ||
|
||
case "text-davinci-003": | ||
case "text-davinci-003": | ||
case "text-davinci-002": | ||
case "code-davinci-002": | ||
case "code-davinci-002": | ||
case "text-curie-001": | ||
case "text-babbage-001": | ||
case "text-ada-001": | ||
case "davinci": | ||
case "curie": | ||
case "babbage": | ||
case "ada": { | ||
return calculateTextCompletionCostInMillicent({ | ||
model, | ||
completion: OpenAITextCompletionSchema.parse(call.rawOutput), | ||
}); | ||
} | ||
|
||
default: { | ||
throw new Error(`Unknown model: ${model}`); | ||
} | ||
} | ||
} |
Oops, something went wrong.