-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial server example implementation (#36)
Co-authored-by: Mike <[email protected]>
- Loading branch information
Showing
4 changed files
with
115 additions
and
1 deletion.
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
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,45 @@ | ||
import { agent } from '@dead-simple-ai-agent/framework/agent' | ||
import { workflow } from '@dead-simple-ai-agent/framework/workflow' | ||
|
||
const nurse = agent({ | ||
role: 'Nurse,doctor assistant', | ||
description: ` | ||
You are skille nurse / doctor assistant. | ||
You role is to cooperate with reporter to create a pre-visit note for a patient that is about to come for a visit. | ||
Ask one question at time up to 5 questions. | ||
Wait until you get the answer. | ||
`, | ||
}) | ||
|
||
const reporter = agent({ | ||
role: 'Reporter', | ||
description: ` | ||
You are skilled at preparing great looking markdown reports. | ||
Prepare a report for a patient that is about to come for a visit. | ||
Add info about the patient's health and symptoms. | ||
`, | ||
tools: {}, | ||
}) | ||
|
||
export const preVisitNoteWorkflow = workflow({ | ||
members: [nurse, reporter], | ||
description: ` | ||
Create a pre-visit note for a patient that is about to come for a visit. | ||
The note should include the patient's health and symptoms. | ||
Include: | ||
- symptoms, | ||
- health issues, | ||
- medications, | ||
- allergies, | ||
- surgeries | ||
Never ask fo: | ||
- personal data, | ||
- sensitive data, | ||
- any data that can be used to identify the patient. | ||
`, | ||
output: ` | ||
A markdown report for the patient's pre-visit note. | ||
`, | ||
}) |
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,68 @@ | ||
/** | ||
* Example borrowed from CrewAI. | ||
*/ | ||
|
||
import { iterate } from '@dead-simple-ai-agent/framework/teamwork' | ||
import { workflowState } from '@dead-simple-ai-agent/framework/workflow' | ||
import fastify, { FastifyReply, FastifyRequest } from 'fastify' | ||
import { promises as fs } from 'fs' | ||
import { tmpdir } from 'os' | ||
import { join } from 'path' | ||
|
||
const server = fastify({ logger: false }) | ||
|
||
import { preVisitNoteWorkflow } from './medical_survey/workflow_server.js' | ||
|
||
const dbPath = (id: string) => join(tmpdir(), id + '_workflow_db.json') | ||
|
||
let state = workflowState(preVisitNoteWorkflow) | ||
|
||
server.post('/start', async () => { | ||
const nextState = await iterate(preVisitNoteWorkflow, state) | ||
|
||
await fs.writeFile(dbPath(nextState.id), JSON.stringify(nextState, null, 2), 'utf-8') | ||
|
||
return { | ||
status: 'running', | ||
state: nextState, | ||
} | ||
}) | ||
|
||
server.post( | ||
'/iterate/:id', | ||
async (req: FastifyRequest<{ Params: { id: string }; Body: { message: string } }>) => { | ||
const { id } = req.params | ||
const { message } = req.body | ||
|
||
const path = dbPath(id) | ||
|
||
if (await fs.exists(path)) { | ||
try { | ||
state = JSON.parse(await fs.readFile(path, 'utf-8')) | ||
console.log('🛟 Loaded workflow from', path) | ||
} catch (error) { | ||
console.log(`🚨Error while loading workflow from ${path}. Starting new workflow.`) | ||
} | ||
} | ||
|
||
if (message) { | ||
// message provided within the call - for example a return call from API/Slack/Whatever | ||
state.messages.push({ role: 'user', content: message }) | ||
} | ||
|
||
const nextState = await iterate(preVisitNoteWorkflow, state) | ||
await fs.writeFile(path, JSON.stringify(nextState, null, 2), 'utf-8') | ||
|
||
return nextState | ||
} | ||
) | ||
|
||
const port = parseInt(process.env['PORT'] || '3000', 10) | ||
server.listen({ | ||
port, | ||
}) | ||
console.log(`🚀 Server running at http://localhost:${port}`) | ||
console.log(`Run 'curl -X POST http://localhost:${port}/start' to start the workflow`) | ||
console.log( | ||
`Run 'curl -X POST http://localhost:${port}/iterate/ID -d '{"message":"Hello"}' to iterate the workflow with the message provided optionally as an answer added to the state` | ||
) |