-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
53 lines (43 loc) · 1.94 KB
/
agent.js
File metadata and controls
53 lines (43 loc) · 1.94 KB
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
import { OpenAI } from "openai";
import { z } from "zod";
import { zodResponseFormat } from 'openai/helpers/zod';
import * as hub from "langchain/hub";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
export const runAgent = async ({ fid, name }, messages) => {
messages = messages.slice(-100);
// Response schema focused on actions
const responseSchema = z.object({
message: z.string().describe("Agent's conversational response"),
action: z.enum([
'find_matches', // Search for potential matches
'accept_match', // User accepts a match
'decline_match', // User declines a match
]).nullable().describe("Action to take"),
user_attributes: z.string().describe(
"Concise but comprehensive profile of the user in natural language. Include:" +
"\n- Current work and role" +
"\n- Key skills and expertise" +
"\n- Notable past experience" +
"\n- Location if known" +
"\n- Any other relevant background"
).nullable(),
user_intent: z.string().describe(
"Clear description of who they want to meet and why, including:" +
"\n- Type of person they're looking for" +
"\n- Desired expertise or background" +
"\n- Purpose of the connection" +
"\n- Any specific requirements"
).nullable(),
});
const agentPrompt = await hub.pull("probably_frens");
const agentPromptText = agentPrompt.promptMessages[0].prompt.template;
const aiResponse = await openai.chat.completions.create({
model: "gpt-4o",
temperature: 0,
messages: [{ role: "system", content: agentPromptText }, ...messages],
response_format: zodResponseFormat(responseSchema, "response"),
stream: false,
});
const response = JSON.parse(aiResponse.choices[0].message.content);
return response;
};