forked from danny-avila/agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
203 lines (170 loc) · 5.87 KB
/
Copy pathindex.ts
File metadata and controls
203 lines (170 loc) · 5.87 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import fs from 'fs/promises';
import { pull } from "langchain/hub";
import { ChatOpenAI } from "@langchain/openai";
import type { ChatPromptTemplate } from "@langchain/core/prompts";
import { AgentExecutor, createOpenAIFunctionsAgent, AgentStep } from "langchain/agents";
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
import type { RunLogPatch } from "@langchain/core/tracers/log_stream";
import dotenv from 'dotenv';
type ExtractedJSONPatchOperation = Pick<RunLogPatch, 'ops'>;
type OperationType = ExtractedJSONPatchOperation extends { ops: (infer T)[] } ? T : never;
// Load environment variables from .env file
dotenv.config();
// Define the tools the agent will have access to.
const tools = [new TavilySearchResults({})];
const llm = new ChatOpenAI({
model: "gpt-3.5-turbo-1106",
temperature: 0,
streaming: true,
});
// Get the prompt to use - you can modify this!
// If you want to see the prompt in full, you can at:
// https://smith.langchain.com/hub/hwchase17/openai-functions-agent
const prompt = await pull<ChatPromptTemplate>(
"hwchase17/openai-functions-agent"
);
const agent = await createOpenAIFunctionsAgent({
llm,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
});
const logStream = await agentExecutor.streamLog({
input: "what are the current US election polls 2024. today is 7/6/24",
});
const finalState: RunLogPatch[] = [];
const outputs: RunLogPatch[] = [];
let accumulatedOutput = '';
let accumulatedArguments = '';
let functionName: string | undefined = undefined;
function processStreamedOutput(op: any) {
let output = '';
if (op.value.text !== undefined) {
output += op.value.text;
}
if (op.value.message && op.value.message.kwargs) {
const kwargs = op.value.message.kwargs;
if (kwargs.content) {
output += kwargs.content;
}
}
if (output) {
accumulatedOutput += output;
process.stdout.write(output);
}
}
// A helper function to handle the event pattern for logged arguments
function handleLoggedArgument(loggedArgument: any) {
if (loggedArgument.value?.message?.additional_kwargs?.function_call) {
const functionCall = loggedArgument.value.message.additional_kwargs.function_call;
if (functionCall.name) {
functionName = functionCall.name;
process.stdout.write(`Logged Function Name:
${JSON.stringify(functionCall, null, 2)}
`);
}
if (functionCall.arguments) {
accumulatedArguments += functionCall.arguments;
// Print the part of the argument as it comes
// process.stdout.write(`Logged Argument: { "arguments": "${functionCall.arguments}" }\n`);
process.stdout.write(`Logged Argument:\n${JSON.stringify(functionCall, null, 2)}`);
}
// Check if the full arguments string has been accumulated
if (accumulatedArguments.startsWith("{") && accumulatedArguments.endsWith("}")) {
// Build the final logged argument string
const completeArguments = accumulatedArguments;
const namePart = functionName ? `"name": "${functionName}", ` : '';
console.log(`\nLogged Argument: {\n ${namePart}"arguments": ${completeArguments}\n}\n`);
// Reset accumulators
accumulatedArguments = '';
functionName = undefined;
}
}
}
for await (const chunk of logStream) {
finalState.push(chunk);
outputs.push(chunk);
if (!chunk.ops) continue;
for (const op of chunk.ops) {
if (isStreamedOutput(op)) {
processStreamedOutput(op);
if (hasFunctionCall(op)) {
handleLoggedArgument(op);
}
} else if (isFinalOutput(op)) {
printFinalOutput(op);
}
}
}
function isStreamedOutput(op: OperationType) {
return op.op === 'add' && (
op.path.includes('/streamed_output/-') ||
op.path.includes('/streamed_output_str/-')
);
}
function hasFunctionCall(op: OperationType) {
return op?.['value']?.message?.additional_kwargs?.function_call;
}
function isFinalOutput(op: OperationType) {
return op.op === 'add' &&
op.value?.output &&
op.path?.startsWith('/logs/') &&
op.path?.endsWith('final_output') &&
!op.path?.includes('Runnable');
}
function printFinalOutput(op: OperationType) {
process.stdout.write(JSON.stringify(op, null, 2));
process.stdout.write(`
########################_START_##########################
${JSON.stringify(op?.['value']?.output, null, 2)}
########################__END__##########################
`);
}
// Define types for the final output structure
interface FinalOutput {
id: string;
streamed_output: Array<{
intermediateSteps?: AgentStep[];
output?: string;
}>;
final_output?: {
output: string;
};
logs: Record<string, any>;
}
// Process finalState to create FinalOutput
const finalOutput: FinalOutput = {
id: '',
streamed_output: [],
logs: {},
};
for (const patch of finalState) {
if (patch.ops) {
for (const op of patch.ops) {
if (op.op === 'add' || op.op === 'replace') {
if (op.path === '/id') {
finalOutput.id = op.value;
} else if (op.path === '/streamed_output/-') {
finalOutput.streamed_output.push(op.value);
} else if (op.path === '/final_output') {
finalOutput.final_output = op.value;
} else if (op.path.startsWith('/logs/')) {
const logKey = op.path.split('/')[2];
finalOutput.logs[logKey] = op.value;
}
}
}
}
}
// Save outputs to a JSON file
await fs.writeFile('outputs.json', JSON.stringify(outputs, null, 2));
console.log("\n\nOutputs have been saved to outputs.json");
// Save the final state separately
await fs.writeFile('final_output.json', JSON.stringify(finalOutput, null, 2));
console.log("\n\nFinal output has been saved to final_output.json");
// Save the cleaned-up accumulated output
await fs.writeFile('cleaned_output.txt', accumulatedOutput);
console.log("\n\nCleaned output has been saved to cleaned_output.txt");