Skip to content

Commit

Permalink
feat: initial server example implementation (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike <[email protected]>
  • Loading branch information
pkarw and grabbou authored Dec 8, 2024
1 parent 52c6da1 commit 1f147fa
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "module",
"dependencies": {
"@dead-simple-ai-agent/framework": "0.0.1",
"@langchain/community": "^0.3.17"
"@langchain/community": "^0.3.17",
"fastify": "^5.1.0"
}
}
45 changes: 45 additions & 0 deletions example/src/medical_survey/workflow_server.ts
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.
`,
})
68 changes: 68 additions & 0 deletions example/src/medical_survey_server.ts
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`
)

0 comments on commit 1f147fa

Please sign in to comment.