-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from statelyai/davidkpiano/api-continued
API improvements, continued
- Loading branch information
Showing
32 changed files
with
3,918 additions
and
4,521 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { z } from 'zod'; | ||
import { createAgent } from '../src'; | ||
import { openai } from '@ai-sdk/openai'; | ||
import { assign, createActor, log, setup } from 'xstate'; | ||
import { getFromTerminal } from './helpers/helpers'; | ||
import { fromDecision } from '../src'; | ||
|
||
const agent = createAgent({ | ||
name: 'chatbot', | ||
model: openai('gpt-4-turbo'), | ||
events: { | ||
'agent.respond': z.object({ | ||
response: z.string().describe('The response from the agent'), | ||
}), | ||
'agent.endConversation': z.object({}).describe('Stop the conversation'), | ||
}, | ||
}); | ||
|
||
const machine = setup({ | ||
types: { | ||
context: {} as { | ||
conversation: string[]; | ||
}, | ||
events: agent.eventTypes, | ||
}, | ||
actors: { agent: fromDecision(agent), getFromTerminal }, | ||
}).createMachine({ | ||
initial: 'waiting', | ||
context: { | ||
conversation: [], | ||
}, | ||
states: { | ||
waiting: { | ||
invoke: { | ||
src: 'getFromTerminal', | ||
input: 'User:', | ||
onDone: { | ||
actions: assign({ | ||
conversation: (x) => | ||
x.context.conversation.concat('User: ' + x.event.output), | ||
}), | ||
target: 'responding', | ||
}, | ||
}, | ||
}, | ||
responding: { | ||
invoke: { | ||
src: 'agent', | ||
input: (x) => ({ | ||
context: { | ||
conversation: x.context.conversation, | ||
}, | ||
goal: 'Respond to the user, unless they want to end the conversation.', | ||
}), | ||
}, | ||
on: { | ||
'agent.respond': { | ||
actions: [ | ||
assign({ | ||
conversation: (x) => | ||
x.context.conversation.concat('Assistant: ' + x.event.response), | ||
}), | ||
log((x) => `Agent: ${x.event.response}`), | ||
], | ||
target: 'waiting', | ||
}, | ||
'agent.endConversation': 'finished', | ||
}, | ||
}, | ||
finished: { | ||
type: 'final', | ||
}, | ||
}, | ||
exit: () => { | ||
console.log('End of conversation.'); | ||
process.exit(); | ||
}, | ||
}); | ||
|
||
createActor(machine).start(); |
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,117 @@ | ||
import { z } from 'zod'; | ||
import { createAgent, fromDecision } from '../src'; | ||
import { openai } from '@ai-sdk/openai'; | ||
import { assign, createActor, setup } from 'xstate'; | ||
import { getFromTerminal } from './helpers/helpers'; | ||
|
||
const agent = createAgent({ | ||
name: 'email', | ||
model: openai('gpt-4'), | ||
events: { | ||
askForClarification: z.object({ | ||
questions: z.array(z.string()).describe('The questions to ask the agent'), | ||
}), | ||
submitEmail: z.object({ | ||
email: z.string().describe('The email to submit'), | ||
}), | ||
}, | ||
}); | ||
|
||
const machine = setup({ | ||
types: { | ||
events: agent.eventTypes, | ||
input: {} as { | ||
email: string; | ||
instructions: string; | ||
}, | ||
context: {} as { | ||
email: string; | ||
instructions: string; | ||
clarifications: string[]; | ||
replyEmail: string | null; | ||
}, | ||
}, | ||
actors: { agent: fromDecision(agent), getFromTerminal }, | ||
}).createMachine({ | ||
initial: 'checking', | ||
context: (x) => ({ | ||
email: x.input.email, | ||
instructions: x.input.instructions, | ||
clarifications: [], | ||
replyEmail: null, | ||
}), | ||
states: { | ||
checking: { | ||
invoke: { | ||
src: 'agent', | ||
input: (x) => ({ | ||
context: { | ||
email: x.context.email, | ||
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.', | ||
}), | ||
}, | ||
on: { | ||
askForClarification: { | ||
actions: (x) => console.log(x.event.questions.join('\n')), | ||
target: 'clarifying', | ||
}, | ||
submitEmail: { | ||
target: 'submitting', | ||
}, | ||
}, | ||
}, | ||
clarifying: { | ||
invoke: { | ||
src: 'getFromTerminal', | ||
input: `Please provide answers to the questions above`, | ||
onDone: { | ||
actions: assign({ | ||
clarifications: (x) => | ||
x.context.clarifications.concat(x.event.output), | ||
}), | ||
target: 'checking', | ||
}, | ||
}, | ||
}, | ||
submitting: { | ||
invoke: { | ||
src: 'agent', | ||
input: ({ context }) => ({ | ||
context: { | ||
email: context.email, | ||
instructions: context.instructions, | ||
clarifications: context.clarifications, | ||
}, | ||
goal: `Create and submit an email based on the instructions.`, | ||
}), | ||
}, | ||
on: { | ||
submitEmail: { | ||
actions: assign({ | ||
replyEmail: ({ event }) => event.email, | ||
}), | ||
target: 'done', | ||
}, | ||
}, | ||
}, | ||
done: { | ||
type: 'final', | ||
entry: (x) => console.log(x.context.replyEmail), | ||
}, | ||
}, | ||
exit: () => { | ||
console.log('End of conversation.'); | ||
process.exit(); | ||
}, | ||
}); | ||
|
||
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.', | ||
}, | ||
}).start(); |
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
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
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
Oops, something went wrong.