Skip to content

Commit

Permalink
Demonstrate Function Calling & Structured Outputs with Zod
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Aug 7, 2024
1 parent cef8794 commit ef54d18
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class ProductsTool extends Tool {
OpenAI's Assistants API introduces a new resource called [Threads](https://platform.openai.com/docs/assistants/how-it-works/managing-threads-and-messages) which messages & files are stored within. Essentially, threads are a managed context window (memory) for your agents. Creating a new thread with Experts.js is as easy as:

```javascript
const thread = Thread.create();
const thread = await Thread.create();
console.log(thread.id) // thread_abc123
```

Expand Down
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"file-type": "^19.0.0",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"p-queue": "^7.4.1"
"p-queue": "^7.4.1",
"zod": "^3.23.8"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export {
AccountsTool,
} from "./fixtures/accountsAssistant.js";
export { HtmlAssistant } from "./fixtures/htmlAssistant.js";
export { CreativeAssistant } from "./fixtures/creativeAssistant.js";
57 changes: 57 additions & 0 deletions test/fixtures/creativeAssistant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { helperName } from "../helpers.js";
import { Tool } from "../../src/experts/tool.js";
import { Assistant } from "../../src/experts/assistant.js";
import { z } from "zod";
import { zodFunction, zodResponseFormat } from "openai/helpers/zod";

class IdeogramTool extends Tool {
constructor() {
super({
name: helperName("IdeogramTool"),
model: "gpt-4o-2024-08-06",
instructions:
"Turn an ideaogram concept into an Ideogram prompt. Very short paragraph.",
temperature: 0.1,
asyncRun: true,
parentsTools: [
zodFunction({
name: "ideogram",
description: "Submit a single concept to cerate an ideogram prompt",
parameters: z.object({
concept: z.object({
concept: z
.string()
.describe("Brief concept in a single sentence."),
thinking: z
.string()
.describe("Your detailed thinking behind the concept."),
}),
}),
}),
],
response_format: zodResponseFormat(
z.object({ prompt: z.string() }),
"prompt"
),
});
}
}

class CreativeAssistant extends Assistant {
constructor() {
super({
name: helperName("CreativeAssistant"),
model: "gpt-4o-2024-08-06",
instructions:
"Take a topic and use your ideogram tool to create three concepts.",
temperature: 0.1,
response_format: zodResponseFormat(
z.object({ prompts: z.array(z.object({ prompt: z.string() })) }),
"prompt"
),
});
this.addAssistantTool(IdeogramTool);
}
}

export { CreativeAssistant };
13 changes: 13 additions & 0 deletions test/uat/arrayOfTools.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { helperThreadID } from "../helpers.js";
import { CreativeAssistant } from "../fixtures.js";

// TODO: Allow these tool calls to be async with a unique thread.

test("can call 3 tools in sequence", async () => {
const threadID = await helperThreadID();
const assistant = await CreativeAssistant.create();
await assistant.ask(
"RouteLLM: What it is and what you should know.",
threadID
);
}, 60000);

0 comments on commit ef54d18

Please sign in to comment.