Skip to content

Commit

Permalink
Merge pull request #31 from statelyai/davidkpiano/agent-api-continued
Browse files Browse the repository at this point in the history
More Agent API work
  • Loading branch information
davidkpiano committed Jun 12, 2024
2 parents 567e846 + a46507e commit 80006b2
Show file tree
Hide file tree
Showing 23 changed files with 1,523 additions and 589 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ out

# Nuxt.js build / generate output
.nuxt
dist
dist/

# Gatsby files
.cache/
Expand Down
13 changes: 12 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@
"request": "launch",
"name": "Debug Current Test File",
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"skipFiles": ["<node_internals>/**", "**/node_modules/**", "examples/**"],
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": ["run", "${relativeFile}"],
"smartStep": true,
"console": "integratedTerminal"
},
{
"type": "node",
"request": "launch",
"name": "Debug Current File",
"program": "${file}",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ts-node",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"sourceMaps": true,
"console": "integratedTerminal"
}
]
}
6 changes: 3 additions & 3 deletions examples/chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ const machine = setup({
},
actors: { agent: fromDecision(agent), getFromTerminal },
}).createMachine({
initial: 'waiting',
initial: 'listening',
context: {
conversation: [],
},
states: {
waiting: {
listening: {
invoke: {
src: 'getFromTerminal',
input: 'User:',
Expand Down Expand Up @@ -62,7 +62,7 @@ const machine = setup({
}),
log((x) => `Agent: ${x.event.response}`),
],
target: 'waiting',
target: 'listening',
},
'agent.endConversation': 'finished',
},
Expand Down
91 changes: 91 additions & 0 deletions examples/cot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { z } from 'zod';
import { createAgent, fromDecision } from '../src';
import { openai } from '@ai-sdk/openai';
import { assign, createActor, log, setup } from 'xstate';
import { getFromTerminal } from './helpers/helpers';

const agent = createAgent({
name: 'chain-of-thought',
model: openai('gpt-4o'),
events: {
'agent.think': z.object({
thought: z
.string()
.describe('The thought process to answering the question'),
}),
'agent.answer': z.object({
answer: z.string().describe('The answer to the question'),
}),
},
});

const machine = setup({
types: {
context: {} as {
question: string | null;
thought: string | null;
},
events: agent.eventTypes,
},
actors: { agent: fromDecision(agent), getFromTerminal },
}).createMachine({
initial: 'asking',
context: {
question: null,
thought: null,
},
states: {
asking: {
invoke: {
src: 'getFromTerminal',
input: 'What would you like to ask?',
onDone: {
actions: assign({
question: (x) => x.event.output,
}),
target: 'thinking',
},
},
},
thinking: {
invoke: {
src: 'agent',
input: (x) => ({
context: x.context,
goal: 'Answer the question. Think step-by-step.',
}),
},
on: {
'agent.think': {
actions: [
log((x) => x.event.thought),
assign({
thought: (x) => x.event.thought,
}),
],
target: 'answering',
},
},
},
answering: {
invoke: {
src: 'agent',
input: (x) => ({
context: x.context,
goal: 'Answer the question',
}),
},
on: {
'agent.answer': {
actions: [log((x) => x.event.answer)],
target: 'answered',
},
},
},
answered: {
type: 'final',
},
},
});

createActor(machine).start();
4 changes: 2 additions & 2 deletions examples/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const machine = setup({
instructions: x.context.instructions,
clarifications: x.context.clarifications,
},
goal: 'Your goal is to respond to the email given the instructions and the provided clarifications. If not enough information is provided, ask for clarification. Otherwise, create and submit a response email.',
goal: 'Your goal is to respond to the email given the instructions and the provided clarifications. If not enough information is provided, ask for clarification. Otherwise, if you are absolutely sure that there is no ambiguous or missing information, create and submit a response email.',
}),
},
on: {
Expand Down Expand Up @@ -112,6 +112,6 @@ createActor(machine, {
input: {
email: 'That sounds great! When are you available?',
instructions:
'Tell them when I am available. Address them by his full (first and last) name.',
'Tell them exactly when I am available. Address them by his full (first and last) name.',
},
}).start();
2 changes: 1 addition & 1 deletion examples/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const machine = setup({
src: 'decider',
input: {
context: true,
goal: 'Determine what the user wants to accomplish. What is their ideal goal state?',
goal: 'Determine what the user wants to accomplish. What is their ideal goal state? ',
maxRetries: 3,
},
},
Expand Down
4 changes: 0 additions & 4 deletions examples/joke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,4 @@ const jokeMachine = setup({

const actor = createActor(jokeMachine);

agent.onMessage((msg) => {
console.log(msg);
});

actor.start();
1 change: 1 addition & 0 deletions examples/raffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const machine = setup({
},
actors: { agent: fromDecision(agent), getFromTerminal },
}).createMachine({
/** @xstate-layout N4IgpgJg5mDOIC5QAoC2BDAxgCwJYDswBKAOjHwBcwAnAqAYggHtCSCA3JgazBLSzyFS5KrXxQEHJpnQVcLANoAGALrKViUAAcmsXHJaaQAD0QAmAOwBmEmYCsADgCMZgJxmALE4ceHSuwA0IACe5gBsZiR29q5KPnaOrmEOAL4pQfw4BMQkEGCiqAR09OgwlCSYTAA2VWCYFACilLRw6kY6egb4RqYIZg52JB52Ya5jSq5+To4eQaEILhYkA0qjVlYeSlsOYWFpGRhZQrn5NIX4xaUiudToAO5tSCAd+vLdT727SiRKHsl2FjCwxcrlmIUQTl2ticHk26ycMPsqXSIEyghyEFud0uZQoJGoYB01AoAHUCIRqI9tLpXoYPohXBYnCQnP4HK4nFYLF5vK45hCPDYmVtdk5uYzuWkUfgmHl4E80dkiO0aV0eogALRhfkIDWDMZjJn9MxhKwjSb7VGHdHCSg0OgqzpvdUIDxmHWc5l2caeKxhVYDFyWxXHPIFIriR2096gXoeVw2VmgiIOBzWJRiqwe2FRdzcpRWE0JPPB61Km73B1PF5q+kIPweEhWMWjCz+BJKJketwkQEWCyuOywv0WaJ2UsCcvY-AUqO12MQls-Ue+Px2KzspweoHLXxsjO-eNmMxSlJAA */
context: {
lastInput: null,
entries: [],
Expand Down
23 changes: 6 additions & 17 deletions examples/wiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,18 @@ const agent = createAgent({
});

async function main() {
const res = await agent.generateText({
const response1 = await agent.generateText({
prompt: 'When was Deadpool 2 released?',
});

console.log(res.text);
console.log(response1.text);

await new Promise((res) => {
setTimeout(() => {
res({});
}, 2000);
const response2 = await agent.generateText({
messages: true,
prompt: 'What about the first one?',
});

console.log(agent.getSnapshot());

const res2 = await agent.generateText({
prompt: agent
.getSnapshot()
.context.history.map((h) => h.content)
.concat('What about the first one?')
.join('\n\n'),
});

console.log(res2.text);
console.log(response2.text);
}

main();
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"@langchain/community": "^0.0.53",
"@langchain/core": "^0.1.63",
"@langchain/openai": "^0.0.28",
"@types/node": "^20.13.0",
"@types/node": "^20.14.2",
"dotenv": "^16.4.5",
"json-schema-to-ts": "^3.1.0",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"tsup": "^8.1.0",
"typescript": "^5.4.5",
"vitest": "^1.6.0",
"wikipedia": "^2.1.2",
Expand All @@ -39,7 +39,8 @@
},
"dependencies": {
"@ai-sdk/openai": "^0.0.13",
"ai": "^3.1.22",
"@xstate/graph": "^2.0.0",
"ai": "^3.1.30",
"xstate": "^5.13.1"
},
"packageManager": "pnpm@8.11.0"
Expand Down
Loading

0 comments on commit 80006b2

Please sign in to comment.