Skip to content

Commit a89ce05

Browse files
committed
incorporate tool
1 parent 95ae175 commit a89ce05

File tree

9 files changed

+245
-46
lines changed

9 files changed

+245
-46
lines changed

ch8/chatbot-real-time/agent.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from langchain.tools import Tool
2+
from langchain.agents import initialize_agent, Tool
3+
from langchain_openai import ChatOpenAI
4+
from langchain.prompts import PromptTemplate
5+
from bytewax.run import cli_main
6+
from dataflow import create_dataflow
7+
from dotenv import load_dotenv
8+
load_dotenv(".env")
9+
import os
10+
11+
openaiapi_key = os.getenv("OPENAI_API_KEY")
12+
13+
def run_dataflow(percentage):
14+
print(percentage)
15+
flow = create_dataflow( percentage)
16+
cli_main(flow)
17+
18+
# Define the LangChain tool
19+
dataflow_tool = Tool(
20+
name="Coinbase Dataflow",
21+
description="Executes the Coinbase dataflow to analyze order book states. Input is percentage for filtering large spreads.",
22+
func=lambda input_text: run_dataflow(*input_text.split(","))
23+
)
24+
25+
# Initialize a language model for the agent
26+
llm = ChatOpenAI(model="gpt-4o-mini", api_key=openaiapi_key)
27+
28+
29+
# Initialize the agent with the tool
30+
agent = initialize_agent(
31+
tools=[dataflow_tool],
32+
llm=llm,
33+
agent="zero-shot-react-description",
34+
verbose=True
35+
)
36+
37+
# Ask a question or give a command
38+
user_query = "Run the Coinbase dataflow with a spread filter of 0.0001"
39+
response = agent.run(user_query)
40+
41+
print(response)

ch8/chatbot-real-time/chatbot.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

ch8/chatbot-real-time/dataflow.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from datetime import timedelta, datetime
1313
from typing import Dict, List, Optional
1414
import ssl
15-
16-
import ssl
15+
import threading
16+
import time
17+
import sys
1718

1819
async def _ws_agen(product_id):
1920
"""Connect to websocket and yield messages as they arrive."""
@@ -153,7 +154,6 @@ def mapper(state, value):
153154
state.update(value)
154155
return (state, state.summarize())
155156

156-
157157
stats = op.stateful_map("orderbook", inp, mapper)
158158

159159
def just_large_spread(prod_summary):
@@ -162,10 +162,12 @@ def just_large_spread(prod_summary):
162162
return summary.spread / summary.ask_price > percentage
163163

164164
filter = op.filter("big_spread", stats, just_large_spread)
165-
166165
op.output("out", filter, StdOutSink())
167-
return flow
166+
167+
return flow
168168

169+
# Example usage
169170
percentage = 0.0001
171+
timer = 5 # Run for 5 seconds
170172
flow = create_dataflow("coinbase", percentage)
171-
cli_main(flow)
173+
cli_main(flow) # Use run_main to handle Bytewax's lifecycle

ch8/chatbot-real-time/experiment_chat.ipynb

Lines changed: 173 additions & 0 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ dependencies = [
4444
"websockets",
4545
"certifi",
4646
"matplotlib",
47-
"langchain"
47+
"langchain",
48+
"langchain-openai"
4849
]
4950

5051
[build-system]

requirements/lib-py3.10.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,9 @@ langchain-core==0.3.20 \
11411141
langchain-openai==0.2.9 \
11421142
--hash=sha256:2723015e56879f9e5edfcb175fdbec6c296c1b3bf65caad28579ce9c4d1bd652 \
11431143
--hash=sha256:38a0f2004f17cdad622d46d4dcfb92d75adbf51909dadc76d0360dd94b0d4f70
1144-
# via ragas
1144+
# via
1145+
# rag-with-haystack (pyproject.toml)
1146+
# ragas
11451147
langchain-text-splitters==0.3.2 \
11461148
--hash=sha256:0db28c53f41d1bc024cdb3b1646741f6d46d5371e90f31e7e7c9fbe75d01c726 \
11471149
--hash=sha256:81e6515d9901d6dd8e35fb31ccd4f30f76d44b771890c789dc835ef9f16204df

requirements/lib-py3.11.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,9 @@ langchain-core==0.3.20 \
11281128
langchain-openai==0.2.9 \
11291129
--hash=sha256:2723015e56879f9e5edfcb175fdbec6c296c1b3bf65caad28579ce9c4d1bd652 \
11301130
--hash=sha256:38a0f2004f17cdad622d46d4dcfb92d75adbf51909dadc76d0360dd94b0d4f70
1131-
# via ragas
1131+
# via
1132+
# rag-with-haystack (pyproject.toml)
1133+
# ragas
11321134
langchain-text-splitters==0.3.2 \
11331135
--hash=sha256:0db28c53f41d1bc024cdb3b1646741f6d46d5371e90f31e7e7c9fbe75d01c726 \
11341136
--hash=sha256:81e6515d9901d6dd8e35fb31ccd4f30f76d44b771890c789dc835ef9f16204df

requirements/lib-py3.12.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,9 @@ langchain-core==0.3.21 \
11191119
langchain-openai==0.2.9 \
11201120
--hash=sha256:2723015e56879f9e5edfcb175fdbec6c296c1b3bf65caad28579ce9c4d1bd652 \
11211121
--hash=sha256:38a0f2004f17cdad622d46d4dcfb92d75adbf51909dadc76d0360dd94b0d4f70
1122-
# via ragas
1122+
# via
1123+
# rag-with-haystack (pyproject.toml)
1124+
# ragas
11231125
langchain-text-splitters==0.3.2 \
11241126
--hash=sha256:0db28c53f41d1bc024cdb3b1646741f6d46d5371e90f31e7e7c9fbe75d01c726 \
11251127
--hash=sha256:81e6515d9901d6dd8e35fb31ccd4f30f76d44b771890c789dc835ef9f16204df

requirements/lib-py3.9.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,13 @@ langchain-core==0.3.22 \
11231123
--hash=sha256:b40b036b3bd4abc1dfc820e149f5a3c4abef2fee83eb807c4310a41a064580e0 \
11241124
--hash=sha256:c61c6d62f137dff2a1ec785473a3225e4fa237e53eac88f5359fda664dc5d91c
11251125
# via
1126+
# langchain-openai
11261127
# langgraph
11271128
# langgraph-checkpoint
1129+
langchain-openai==0.2.12 \
1130+
--hash=sha256:8b92096623065a2820e89aa5fb0a262fb109d56c346e3b09ba319af424c45cd1 \
1131+
--hash=sha256:916965c45584d9ea565825ad3bb7629b1ff57f12f36d4b937e5b7d65903839d6
1132+
# via rag-with-haystack (pyproject.toml)
11281133
langcodes==3.5.0 \
11291134
--hash=sha256:1eef8168d07e51e131a2497ffecad4b663f6208e7c3ae3b8dc15c51734a6f801 \
11301135
--hash=sha256:853c69d1a35e0e13da2f427bb68fb2fa4a8f4fb899e0c62ad8df8d073dcfed33
@@ -1795,11 +1800,12 @@ numpy==2.0.2 \
17951800
# spacy
17961801
# thinc
17971802
# transformers
1798-
openai==1.55.0 \
1799-
--hash=sha256:446e08918f8dd70d8723274be860404c8c7cc46b91b93bbc0ef051f57eb503c1 \
1800-
--hash=sha256:6c0975ac8540fe639d12b4ff5a8e0bf1424c844c4a4251148f59f06c4b2bd5db
1803+
openai==1.57.1 \
1804+
--hash=sha256:3865686c927e93492d1145938d4a24b634951531c4b2769d43ca5dbd4b25d8fd \
1805+
--hash=sha256:a95f22e04ab3df26e64a15d958342265e802314131275908b3b3e36f8c5d4377
18011806
# via
18021807
# haystack-ai
1808+
# langchain-openai
18031809
# ragas
18041810
orjson==3.10.12 \
18051811
--hash=sha256:0000758ae7c7853e0a4a6063f534c61656ebff644391e1f81698c1b2d2fc8cd2 \
@@ -3320,7 +3326,9 @@ tiktoken==0.8.0 \
33203326
--hash=sha256:d8f3192733ac4d77977432947d563d7e1b310b96497acd3c196c9bddb36ed9db \
33213327
--hash=sha256:f13d13c981511331eac0d01a59b5df7c0d4060a8be1e378672822213da51e0a2 \
33223328
--hash=sha256:fe9399bdc3f29d428f16a2f86c3c8ec20be3eac5f53693ce4980371c3245729b
3323-
# via ragas
3329+
# via
3330+
# langchain-openai
3331+
# ragas
33243332
tld==0.13 \
33253333
--hash=sha256:93dde5e1c04bdf1844976eae440706379d21f4ab235b73c05d7483e074fb5629 \
33263334
--hash=sha256:f75b2be080f767ed17c2338a339eaa4fab5792586319ca819119da252f9f3749

0 commit comments

Comments
 (0)