-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
177 lines (150 loc) · 5.55 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import OpenAI from 'openai';
require('dotenv').config()
import express from 'express';
import cors from 'cors';
const app = express();
const port = 3000;
app.use(express.text());
app.use(cors());
const client = new OpenAI({ apiKey: process.env.OPENAI_API });
if (!client) {
throw new Error('No OpenAI API key found');
}
//TODO: ZOD validation for the function arguments
//TODO: Add a function to handle the user message and return the doctor details
//TODO: Response should be in json format
//TODO: Response should be uni-directional, currently it is asking for user input after the first response
interface Doctor {
name: string;
type: string;
consultation_mode: string;
expertise_description: string;
}
interface ReferToDocArgs {
type: string;
consultation_mode: string;
expertise_description: string;
}
type userMessage = string | null;
async function main() {
const runner = client.beta.chat.completions
.runTools({
model: "gpt-3.5-turbo-0125",
messages: [
{
role: "system",
content: `You are a helpful assistant that can refer patients to doctors. You have a function called ReferToDoc that takes a type of doctor, a consultation mode, and an expertise description, and returns a doctor that fits those criteria.`
},
{
role: "user",
content: "common illness"
}
],
tools: [
{
type: "function",
function: {
function: ReferToDoc,
parse: JSON.parse,
description: "Refers a patient to a doctor based on the type of doctor, consultation mode, and expertise description",
parameters: {
type: "object",
properties: {
type: {
type: "string",
enum: [
"General Physician",
"Specialist"
],
description: "The type of doctor to refer to"
},
consultation_mode: {
type: "string",
enum: [
"Physical Examination",
"Virtual Consultation"
],
description: "The mode of consultation preferred"
},
expertise_description: {
type: "string",
description: "A brief description of the required expertise for the doctor"
}
},
required: [
"type",
"consultation_mode",
"expertise_description"
]
}
}
}
],
max_tokens: 150,
temperature: 0.7,
stop: ["\n"]
});
runner.on('message', async (message) => {
// console.log(message);
});
try {
const finalContent = await runner.finalContent();
console.log('Response:', finalContent);
return finalContent;
} catch (error) {
console.error("Error:", error);
return error;
}
}
async function ReferToDoc(args: ReferToDocArgs): Promise<Doctor> {
const doctors: Doctor[] = [
{
name: 'Dr. Smith',
type: 'General Physician',
consultation_mode: 'Physical Examination',
expertise_description: 'General health checkup and common illnesses',
},
{
name: 'Dr. Johnson',
type: 'General Physician',
consultation_mode: 'Virtual Consultation',
expertise_description: 'General health checkup and common illnesses',
},
{
name: 'Dr. Lee',
type: 'Specialist',
consultation_mode: 'Physical Examination',
expertise_description: 'Orthopedic and sports injuries',
},
{
name: 'Dr. Patel',
type: 'Specialist',
consultation_mode: 'Virtual Consultation',
expertise_description: 'Orthopedic and sports injuries',
},
];
const doctor = doctors.find(
(doctor) =>
doctor.type === args.type &&
doctor.consultation_mode === args.consultation_mode &&
doctor.expertise_description.includes(args.expertise_description)
);
if (!doctor) {
throw new Error(`No doctor found that matches the criteria: Type - ${args.type}, Consultation Mode - ${args.consultation_mode}, Expertise Description - ${args.expertise_description}`);
}
return doctor;
}
main();
app.get('/', async (req, res) => {
try {
const result = await main();
const message = result instanceof Error ? result.message : result;
res.send(message);
} catch (error: any) {
console.error(error);
res.status(500).send(error.message);
}
});
app.listen(port, () => {
console.log('Started proxy express server');
});