-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
106 lines (97 loc) · 2.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import OpenAI from 'openai';
import { zodResponseFormat } from 'openai/helpers/zod';
import { z } from 'zod';
import * as ChatAPI from 'openai/src/resources/chat/chat';
type ChatModelPrices = {
inputPrice: number;
outputPrice: number;
}
// Updated on 2024-09-23
const modelPrices: {[index: string | ChatAPI.ChatModel]: ChatModelPrices} = {
'gpt-4o': {
inputPrice: 0.000005,
outputPrice: 0.000015,
},
'gpt-4o-mini': {
inputPrice: 0.00000015,
outputPrice: 0.0000006,
},
}
const calculatePrice = (usage: OpenAI.Completions.CompletionUsage, model: string | ChatAPI.ChatModel) => {
const modelPrice = modelPrices[model];
if (!modelPrice) {
return null;
}
const pricing = {
prompt_tokens: usage.prompt_tokens,
prompt_price: usage.prompt_tokens * modelPrice.inputPrice,
completion_tokens: usage.completion_tokens,
completion_price: usage.completion_tokens * modelPrice.outputPrice,
total_tokens: usage.prompt_tokens + usage.completion_tokens,
total_price: usage.prompt_tokens * modelPrice.inputPrice + usage.completion_tokens * modelPrice.outputPrice
};
return pricing;
};
export const openAiRequestWithSchema = async <T>(
model: string | ChatAPI.ChatModel,
messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[],
schema: z.ZodType<T>,
showPrices = false
): Promise<T | null> => {
const client = new OpenAI();
const responseFormat = zodResponseFormat(schema, 'schema');
try {
const completion = await client.beta.chat.completions.parse({
model,
messages,
response_format: responseFormat
});
if (completion.usage && showPrices) {
console.log('Usage and Prices:', calculatePrice(completion.usage, model));
}
if (completion.choices[0].message.content === null) {
return null;
}
const parsedArguments = schema.parse(JSON.parse(completion.choices[0].message.content));
return parsedArguments as T;
} catch (error) {
console.error('Error calling OpenAI:', error);
return null;
}
};
export const giveMeAiMessages = ({
system,
user
}: {
system: string;
user: string;
}): OpenAI.Chat.Completions.ChatCompletionMessageParam[] => {
return [
{
role: 'system',
content: system
},
{
role: 'user',
content: user
}
];
};
export const craftJsonSchemaPrompt = ({
instructions,
fieldDescriptions,
exampleJson
}: {
instructions: string[];
fieldDescriptions: string[];
exampleJson: string;
}) => {
return `
## INSTRUCTIONS:
${instructions.join('\n')}
## FIELDS DESCRIPTION:
${fieldDescriptions.join('\n')}
## EXAMPLE JSON:
${exampleJson}
`;
};