Skip to content

Commit

Permalink
Add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
holdenmatt committed Dec 22, 2023
1 parent 39643a7 commit f51ff9c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 36 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "openai-zod-functions",
"version": "0.1.0",
"description": "OpenAI Function Calling in Typescript using Zod.",
"description": "OpenAI Function Calling in Typescript using Zod",
"author": "Matt Holden",
"license": "MIT",
"private": false,
Expand Down Expand Up @@ -31,7 +31,11 @@
},
"devDependencies": {
"openai": "^4.24.1",
"prettier": "^3.1.1",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
},
"prettier": {
"printWidth": 90
}
}
10 changes: 6 additions & 4 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ type JsonSchemaFunctionDef = {
/**
* Convert a ZodFunctionDef to JSON Schema.
*/
export function toJsonSchema(
functionDef: ZodFunctionDef
): JsonSchemaFunctionDef {
export function toJsonSchema(functionDef: ZodFunctionDef): JsonSchemaFunctionDef {
const { name, description, schema } = functionDef;

const jsonSchema = zodToJsonSchema(schema) as JsonSchema7ObjectType;
Expand All @@ -47,7 +45,11 @@ export function toJsonSchema(
return {
name,
description,
parameters: { type, properties, required },
parameters: {
type,
properties,
required,
},
};
}

Expand Down
22 changes: 9 additions & 13 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ export class FunctionHandlerError extends HttpError {
* The handler takes the parsed/typed function parameters as arguments,
* and can perform any (async) computation and/or return an arbitrary output.
*/
export type ZodFunctionHandler<Parameters, Output> =
ZodFunctionDef<Parameters> & {
handler: (parameters: Parameters) => Promise<Output>;
};
export type ZodFunctionHandler<Parameters, Output> = ZodFunctionDef<Parameters> & {
handler: (parameters: Parameters) => Promise<Output>;
};

/**
* Create a ZodFunctionHandler.
*
* You can also create one directly, but this convenience function
* infers type parameters automatically from the schema/handler types.
*/
export function createFunctionHandler<
Schema extends z.ZodType<any, any, any>,
Output
>({
export function createFunctionHandler<Schema extends z.ZodType<any, any, any>, Output>({
name,
description,
schema,
Expand All @@ -57,7 +53,7 @@ export function createFunctionHandler<
*/
function findHandler<Parameters, Output>(
name: string,
handlers: ZodFunctionHandler<Parameters, Output>[]
handlers: ZodFunctionHandler<Parameters, Output>[],
): ZodFunctionHandler<Parameters, Output> {
const handler = handlers.find((h) => h.name === name);
if (!handler) {
Expand All @@ -74,7 +70,7 @@ function findHandler<Parameters, Output>(
*/
async function handleSingleToolCall<Output>(
handlers: ZodFunctionHandler<any, Output>[],
toolCall: ChatCompletionMessageToolCall
toolCall: ChatCompletionMessageToolCall,
): Promise<Output> {
const { name } = toolCall.function;

Expand All @@ -83,7 +79,7 @@ async function handleSingleToolCall<Output>(
const parameters = parseArguments(
toolCall.function.name,
toolCall.function.arguments,
handler.schema
handler.schema,
);

try {
Expand All @@ -104,14 +100,14 @@ async function handleSingleToolCall<Output>(
*/
export async function handleToolCalls<Output>(
handlers: ZodFunctionHandler<any, Output>[],
toolCalls: ChatCompletionMessageToolCall[] | undefined
toolCalls: ChatCompletionMessageToolCall[] | undefined,
): Promise<Output[]> {
if (toolCalls === undefined) {
return [];
}

const toolOutputs = await Promise.all(
toolCalls.map((toolCall) => handleSingleToolCall(handlers, toolCall))
toolCalls.map((toolCall) => handleSingleToolCall(handlers, toolCall)),
);

return toolOutputs;
Expand Down
10 changes: 3 additions & 7 deletions src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { findFunction, parseArguments } from "./utils/parse";
type FunctionReducer<State, Parameters = any> = (
prevState: State,
functionName: string,
args: Parameters
args: Parameters,
) => State;

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ export type ZodFunctionReducer<State> = {
export async function reduceToolCalls<State>(
prevState: State,
functionReducer: ZodFunctionReducer<State>,
toolCalls: ChatCompletionMessageToolCall[] | undefined
toolCalls: ChatCompletionMessageToolCall[] | undefined,
): Promise<State> {
if (toolCalls === undefined) {
return prevState;
Expand All @@ -57,11 +57,7 @@ export async function reduceToolCalls<State>(
const { name } = toolCall.function;
const func = findFunction(name, functionReducer.functions);

const parameters = parseArguments(
name,
toolCall.function.arguments,
func.schema
);
const parameters = parseArguments(name, toolCall.function.arguments, func.schema);

try {
logger.info("Calling reducer: ", name);
Expand Down
9 changes: 3 additions & 6 deletions src/utils/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { logger } from "./logging";
* Assert that a ChatCompletion response contains a single tool_call.
* Throw an error if it doesn't.
*/
export function assertSingleToolCall(
completion: ChatCompletion,
label?: string
): void {
export function assertSingleToolCall(completion: ChatCompletion, label?: string): void {
const prefix = label ? `Unexpected (${label})` : "Unexpected";

if (completion.choices.length !== 1) {
Expand All @@ -26,8 +23,8 @@ export function assertSingleToolCall(
`${prefix}: Multiple tool calls for ${label}: ${JSON.stringify(
tool_calls,
null,
2
)}`
2,
)}`,
);
throw new Error(`${prefix}: got ${tool_calls.length} tool_calls`);
}
Expand Down
7 changes: 2 additions & 5 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ export class InvalidFunctionArguments extends HttpError {
*
* Throws InvalidFunctionName if no function with that name is found.
*/
export function findFunction(
name: string,
functions: ZodFunctionDef[]
): ZodFunctionDef {
export function findFunction(name: string, functions: ZodFunctionDef[]): ZodFunctionDef {
const func = functions.find((f) => f.name === name);
if (!func) {
throw new InvalidFunctionName(name);
Expand All @@ -54,7 +51,7 @@ export function findFunction(
export function parseArguments<Parameters>(
name: string,
args: string,
schema: z.ZodType<Parameters, any, any>
schema: z.ZodType<Parameters, any, any>,
): Parameters {
// Parse the arguments string to JSON (should be guaranteed)
const parameters = JSON.parse(args);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,11 @@ postcss-load-config@^4.0.1:
lilconfig "^3.0.0"
yaml "^2.3.4"

prettier@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.1.1.tgz#6ba9f23165d690b6cbdaa88cb0807278f7019848"
integrity sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw==

punycode@^2.1.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5"
Expand Down

0 comments on commit f51ff9c

Please sign in to comment.