-
Notifications
You must be signed in to change notification settings - Fork 12
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 #51 from statelyai/davidkpiano/middleware
Middleware
- Loading branch information
Showing
36 changed files
with
1,808 additions
and
1,496 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,9 @@ | ||
--- | ||
'@statelyai/agent': major | ||
--- | ||
|
||
- `agent.generateText(…)` is removed in favor of using the AI SDK's `generateText(…)` function with a wrapped model. | ||
- `agent.streamText(…)` is removed in favor of using the AI SDK's `streamText(…)` function with a wrapped model. | ||
- Custom adapters are removed for now, but may be re-added in future releases. Using the AI SDK is recommended for now. | ||
- Correlation IDs are removed in favor of using [OpenTelemetry with the AI SDK](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry#telemetry). | ||
- The `createAgentMiddleware(…)` function was introduced to facilitate agent message history. You can also use `agent.wrap(model)` to wrap a model with Stately Agent middleware. |
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,57 @@ | ||
import { z } from 'zod'; | ||
import { createAgent } from '../src'; | ||
import { openai } from '@ai-sdk/openai'; | ||
import { getFromTerminal } from './helpers/helpers'; | ||
|
||
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'), | ||
}, | ||
context: { | ||
userMessage: z.string(), | ||
}, | ||
}); | ||
|
||
async function main() { | ||
let status = 'listening'; | ||
let userMessage = ''; | ||
|
||
while (status !== 'finished') { | ||
switch (status) { | ||
case 'listening': | ||
userMessage = await getFromTerminal('User:'); | ||
status = 'responding'; | ||
break; | ||
|
||
case 'responding': | ||
const decision = await agent.decide({ | ||
messages: agent.getMessages(), | ||
goal: 'Respond to the user, unless they want to end the conversation.', | ||
state: { | ||
value: status, | ||
context: { | ||
userMessage: 'User says: ' + userMessage, | ||
}, | ||
}, | ||
}); | ||
|
||
if (decision?.nextEvent?.type === 'agent.respond') { | ||
console.log(`Agent: ${decision.nextEvent.response}`); | ||
status = 'listening'; | ||
} else if (decision?.nextEvent?.type === 'agent.endConversation') { | ||
status = 'finished'; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
console.log('End of conversation.'); | ||
process.exit(); | ||
} | ||
|
||
main().catch(console.error); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { openai } from '@ai-sdk/openai'; | ||
import { createAgent, fromDecision } from '../src'; | ||
import { assign, createActor, createMachine, fromPromise } from 'xstate'; | ||
import { z } from 'zod'; | ||
import { fromTerminal } from './helpers/helpers'; | ||
|
||
const agent = createAgent({ | ||
model: openai('gpt-4o-mini'), | ||
events: { | ||
getTime: z.object({}).describe('Get the current time'), | ||
other: z.object({}).describe('Do something else'), | ||
}, | ||
}); | ||
|
||
const machine = createMachine({ | ||
initial: 'start', | ||
context: { | ||
question: null, | ||
}, | ||
states: { | ||
start: { | ||
invoke: { | ||
src: fromTerminal, | ||
input: 'What do you want to do?', | ||
onDone: { | ||
actions: assign({ | ||
question: ({ event }) => event.output, | ||
}), | ||
target: 'deciding', | ||
}, | ||
}, | ||
}, | ||
deciding: { | ||
invoke: { | ||
src: fromDecision(agent), | ||
input: { | ||
goal: 'Satisfy the user question', | ||
context: true, | ||
}, | ||
}, | ||
on: { | ||
getTime: 'gettingTime', | ||
other: 'other', | ||
}, | ||
}, | ||
gettingTime: { | ||
invoke: { | ||
src: fromPromise(async () => { | ||
console.log('Time:', new Date().toLocaleTimeString()); | ||
}), | ||
onDone: 'start', | ||
}, | ||
}, | ||
other: { | ||
entry: () => | ||
console.log( | ||
'You want me to do something else. I can only tell the time.' | ||
), | ||
after: { | ||
1000: 'start', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
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
Oops, something went wrong.