Created
December 9, 2024 02:52
-
-
Save smlparry/8aebaadd82f5b1d87be9d2f91a001af6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import OpenAI from "finetunedb/openai"; | |
| import { Env, Message, CompletionUsage } from "@/types"; | |
| export const MODELS = { | |
| GPT_35: "gpt-3.5-turbo-0125", | |
| GPT_4: "gpt-4-1106-preview", | |
| FINE_TUNE: "ft:gpt-3.5-turbo-0125:fitter::8yddZqX0", | |
| GPT_4o_MINI: "gpt-4o-mini", | |
| } as const; | |
| export type Model = (typeof MODELS)[keyof typeof MODELS]; | |
| export const COSTS = { | |
| [MODELS.GPT_35]: { | |
| PROMPT: 0.001, // Per thousand tokens | |
| COMPLETION: 0.002, | |
| }, | |
| [MODELS.FINE_TUNE]: { | |
| PROMPT: 0.003, // Per thousand tokens | |
| COMPLETION: 0.006, | |
| }, | |
| [MODELS.GPT_4]: { | |
| PROMPT: 0.01, // Per thousand tokens | |
| COMPLETION: 0.03, | |
| }, | |
| [MODELS.GPT_4o_MINI]: { | |
| PROMPT: 0.00015, // Per thousand tokens | |
| COMPLETION: 0.0006, | |
| }, | |
| }; | |
| class OpenAiClient { | |
| apiKey: string; | |
| model: string; | |
| openai: OpenAI; | |
| env: "development" | "staging" | "production"; | |
| // baseUrl: string; | |
| constructor(env: Env) { | |
| // TODO: Decide whether we use Cloudflare or FinetuneDB | |
| // this.baseUrl = | |
| // "https://gateway.ai.cloudflare.com/v1/324b4b555acf9237253d01385a218689/movement-ai-dev/openai"; | |
| this.apiKey = env.OPENAI_API_KEY; | |
| this.model = env.AI_MODEL; | |
| this.env = env.ENV; | |
| this.openai = new OpenAI({ | |
| apiKey: this.apiKey, | |
| finetunedb: { | |
| projectId: env.FINETUNEDB_PROJECT_ID, | |
| apiKey: env.FINETUNEDB_API_KEY, | |
| }, | |
| }); | |
| } | |
| async run( | |
| { | |
| messages, | |
| stream = false, | |
| jsonMode = false, | |
| metadata, | |
| }: { | |
| messages: Array<Message>; | |
| stream?: boolean; | |
| jsonMode?: boolean; | |
| metadata?: Record<string, string | number | null | undefined>; | |
| }, | |
| callback: (value: string, usage: CompletionUsage | undefined) => void = () => {} | |
| ) { | |
| const response = await this.openai.chat.completions.create({ | |
| model: this.model, | |
| messages, | |
| response_format: { type: "json_object" }, | |
| finetunedb: { | |
| logRequest: true, | |
| }, | |
| }); | |
| console.log("------ Usage Report ------"); | |
| console.log("Model Used:", response.model); | |
| console.log("Prompt Tokens:", response.usage?.prompt_tokens); | |
| console.log("Completion Tokens:", response.usage?.completion_tokens); | |
| console.log("Total Tokens:", response.usage?.total_tokens); | |
| console.log("------------"); | |
| return callback( | |
| response.choices[0].message.content || "", | |
| response.usage | |
| ? { | |
| ...(response.usage || {}), | |
| model: response.model, | |
| } | |
| : undefined | |
| ); | |
| } | |
| } | |
| export default OpenAiClient; |
This file contains hidden or 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
| import OpenAI from "finetunedb/openai"; | |
| import { Env, Message, CompletionUsage } from "@/types"; | |
| export const MODELS = { | |
| GPT_35: "gpt-3.5-turbo-0125", | |
| GPT_4: "gpt-4-1106-preview", | |
| FINE_TUNE: "ft:gpt-3.5-turbo-0125:fitter::8yddZqX0", | |
| GPT_4o_MINI: "gpt-4o-mini", | |
| } as const; | |
| export type Model = (typeof MODELS)[keyof typeof MODELS]; | |
| export const COSTS = { | |
| [MODELS.GPT_35]: { | |
| PROMPT: 0.001, // Per thousand tokens | |
| COMPLETION: 0.002, | |
| }, | |
| [MODELS.FINE_TUNE]: { | |
| PROMPT: 0.003, // Per thousand tokens | |
| COMPLETION: 0.006, | |
| }, | |
| [MODELS.GPT_4]: { | |
| PROMPT: 0.01, // Per thousand tokens | |
| COMPLETION: 0.03, | |
| }, | |
| [MODELS.GPT_4o_MINI]: { | |
| PROMPT: 0.00015, // Per thousand tokens | |
| COMPLETION: 0.0006, | |
| }, | |
| }; | |
| class OpenAiClient { | |
| apiKey: string; | |
| model: string; | |
| openai: OpenAI; | |
| env: "development" | "staging" | "production"; | |
| // baseUrl: string; | |
| constructor(env: Env) { | |
| // TODO: Decide whether we use Cloudflare or FinetuneDB | |
| // this.baseUrl = | |
| // "https://gateway.ai.cloudflare.com/v1/324b4b555acf9237253d01385a218689/movement-ai-dev/openai"; | |
| this.apiKey = env.OPENAI_API_KEY; | |
| this.model = env.AI_MODEL; | |
| this.env = env.ENV; | |
| this.openai = new OpenAI({ | |
| apiKey: this.apiKey, | |
| finetunedb: { | |
| projectId: env.FINETUNEDB_PROJECT_ID, | |
| apiKey: env.FINETUNEDB_API_KEY, | |
| }, | |
| }); | |
| } | |
| async run( | |
| { | |
| messages, | |
| stream = false, | |
| jsonMode = false, | |
| metadata, | |
| }: { | |
| messages: Array<Message>; | |
| stream?: boolean; | |
| jsonMode?: boolean; | |
| metadata?: Record<string, string | number | null | undefined>; | |
| }, | |
| callback: (value: string, usage: CompletionUsage | undefined) => void = () => {} | |
| ) { | |
| const response = await this.openai.chat.completions.create({ | |
| model: this.model, | |
| messages, | |
| // response_format: { type: "json_object" }, | |
| finetunedb: { | |
| logRequest: true, | |
| }, | |
| }); | |
| console.log("------ Usage Report ------"); | |
| console.log("Model Used:", response.model); | |
| console.log("Prompt Tokens:", response.usage?.prompt_tokens); | |
| console.log("Completion Tokens:", response.usage?.completion_tokens); | |
| console.log("Total Tokens:", response.usage?.total_tokens); | |
| console.log("------------"); | |
| return callback( | |
| response.choices[0].message.content || "", | |
| response.usage | |
| ? { | |
| ...(response.usage || {}), | |
| model: response.model, | |
| } | |
| : undefined | |
| ); | |
| } | |
| } | |
| export default OpenAiClient; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment