From 5dd3b3308fb63a0a6e84c765b4a05ad749c28b2c Mon Sep 17 00:00:00 2001 From: isamu Date: Sat, 11 May 2024 05:56:49 +0900 Subject: [PATCH] add openai streaming sample --- samples/streaming/openai.ts | 50 +++++++++++++++++++ .../openai_agent.ts} | 0 2 files changed, 50 insertions(+) create mode 100644 samples/streaming/openai.ts rename samples/{openai_agent_test.ts => streaming/openai_agent.ts} (100%) diff --git a/samples/streaming/openai.ts b/samples/streaming/openai.ts new file mode 100644 index 000000000..e2e8d1a80 --- /dev/null +++ b/samples/streaming/openai.ts @@ -0,0 +1,50 @@ +import "dotenv/config"; + +import { GraphAI } from "@/graphai"; + +import { AgentFunctionContext } from "@/type"; +import { openAIAgent } from "@/experimental_agents/llm_agents/openai_agent"; +import { streamAgentFilterGenerator } from "@/experimental_agent_filters/stream"; + +const streamData: Record = {}; + +const outSideFunciton = (context: AgentFunctionContext, data: string) => { + const nodeId = context.debugInfo.nodeId; + streamData[nodeId] = (streamData[nodeId] || "") + data; + console.log(streamData); +}; + +const agentFilters = [ + { + name: "streamAgentFilter", + agent: streamAgentFilterGenerator(outSideFunciton), + }, +]; + + +const graph_data = { + version: 0.3, + nodes: { + node1: { + value: "Please tell me about photosynthesis in 50 words.", + }, + node2: { + agent: "openAIAgent", + inputs: [":node1"], + isResult: true, + }, + }, +}; + +export const main = async () => { + // const result = await graphDataTestRunner(__filename, graph_data, { openAIAgent }, { agentFilters }); + const graph = new GraphAI(graph_data, { openAIAgent }, { agentFilters }); + const result = await graph.run(); + console.log(JSON.stringify(result)); + + console.log("COMPLETE 1"); +}; + +if (process.argv[1] === __filename) { + main(); +} diff --git a/samples/openai_agent_test.ts b/samples/streaming/openai_agent.ts similarity index 100% rename from samples/openai_agent_test.ts rename to samples/streaming/openai_agent.ts