-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 compute token usage for function / tool streaming
- Loading branch information
Showing
5 changed files
with
364 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CompletionUsage } from 'openai/resources'; | ||
|
||
import { AgentModelName } from '@chaindesk/prisma'; | ||
|
||
import { ModelConfig } from './config'; | ||
|
||
type Props = {}; | ||
|
||
function getUsageCost(props: { | ||
modelName: AgentModelName; | ||
usage: CompletionUsage; | ||
}) { | ||
return ( | ||
(props.usage?.prompt_tokens || 0) * | ||
ModelConfig[props.modelName]?.providerPriceByInputToken + | ||
(props.usage?.completion_tokens || 0) * | ||
ModelConfig[props.modelName]?.providerPricePriceByOutputToken | ||
); | ||
} | ||
export default getUsageCost; |
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,126 @@ | ||
import OpenAI from 'openai'; | ||
|
||
type OpenAIFunction = OpenAI.Chat.ChatCompletionCreateParams.Function; | ||
|
||
// Types representing the OpenAI function definitions. While the OpenAI client library | ||
// does have types for function definitions, the properties are just Record<string, unknown>, | ||
// which isn't very useful for type checking this formatting code. | ||
export interface FunctionDef extends Omit<OpenAIFunction, 'parameters'> { | ||
name: string; | ||
description?: string; | ||
parameters: ObjectProp; | ||
} | ||
|
||
interface ObjectProp { | ||
type: 'object'; | ||
properties?: { | ||
[key: string]: Prop; | ||
}; | ||
required?: string[]; | ||
} | ||
|
||
interface AnyOfProp { | ||
anyOf: Prop[]; | ||
} | ||
|
||
type Prop = { | ||
description?: string; | ||
} & ( | ||
| AnyOfProp | ||
| ObjectProp | ||
| { | ||
type: 'string'; | ||
enum?: string[]; | ||
} | ||
| { | ||
type: 'number' | 'integer'; | ||
minimum?: number; | ||
maximum?: number; | ||
enum?: number[]; | ||
} | ||
| { type: 'boolean' } | ||
| { type: 'null' } | ||
| { | ||
type: 'array'; | ||
items?: Prop; | ||
} | ||
); | ||
|
||
function isAnyOfProp(prop: Prop): prop is AnyOfProp { | ||
return ( | ||
(prop as AnyOfProp).anyOf !== undefined && | ||
Array.isArray((prop as AnyOfProp).anyOf) | ||
); | ||
} | ||
|
||
// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas. | ||
// This function converts the JSON schemas into TypeScript definitions. | ||
export function formatFunctionDefinitions(functions: FunctionDef[]) { | ||
const lines = ['namespace functions {', '']; | ||
for (const f of functions) { | ||
if (f.description) { | ||
lines.push(`// ${f.description}`); | ||
} | ||
if (Object.keys(f.parameters.properties ?? {}).length > 0) { | ||
lines.push(`type ${f.name} = (_: {`); | ||
lines.push(formatObjectProperties(f.parameters, 0)); | ||
lines.push('}) => any;'); | ||
} else { | ||
lines.push(`type ${f.name} = () => any;`); | ||
} | ||
lines.push(''); | ||
} | ||
lines.push('} // namespace functions'); | ||
return lines.join('\n'); | ||
} | ||
|
||
// Format just the properties of an object (not including the surrounding braces) | ||
function formatObjectProperties(obj: ObjectProp, indent: number): string { | ||
const lines = []; | ||
for (const [name, param] of Object.entries(obj.properties ?? {})) { | ||
if (param.description && indent < 2) { | ||
lines.push(`// ${param.description}`); | ||
} | ||
if (obj.required?.includes(name)) { | ||
lines.push(`${name}: ${formatType(param, indent)},`); | ||
} else { | ||
lines.push(`${name}?: ${formatType(param, indent)},`); | ||
} | ||
} | ||
return lines.map((line) => ' '.repeat(indent) + line).join('\n'); | ||
} | ||
|
||
// Format a single property type | ||
function formatType(param: Prop, indent: number): string { | ||
if (isAnyOfProp(param)) { | ||
return param.anyOf.map((v) => formatType(v, indent)).join(' | '); | ||
} | ||
switch (param.type) { | ||
case 'string': | ||
if (param.enum) { | ||
return param.enum.map((v) => `"${v}"`).join(' | '); | ||
} | ||
return 'string'; | ||
case 'number': | ||
if (param.enum) { | ||
return param.enum.map((v) => `${v}`).join(' | '); | ||
} | ||
return 'number'; | ||
case 'integer': | ||
if (param.enum) { | ||
return param.enum.map((v) => `${v}`).join(' | '); | ||
} | ||
return 'number'; | ||
case 'boolean': | ||
return 'boolean'; | ||
case 'null': | ||
return 'null'; | ||
case 'object': | ||
return ['{', formatObjectProperties(param, indent + 2), '}'].join('\n'); | ||
case 'array': | ||
if (param.items) { | ||
return `${formatType(param.items, indent)}[]`; | ||
} | ||
return 'any[]'; | ||
} | ||
} |
Oops, something went wrong.
8bb36b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dashboard – ./
databerry.chat
dashboard-nu-tawny.vercel.app
*.databerry.ai
www.chatbotgpt.ai
www.chaindesk.ai
*.chaindesk.chat
databerry.ai
chaindesk.ai
*.chaindesk.ai
www.resolveai.io
chaindesk.chat
resolveai.io
www.databerry.ai
dashboard-databerry.vercel.app
bestaichatbot.org
www.bestaichatbot.org
dashboard-git-main-databerry.vercel.app