feat: solution for 'Untitled Task'
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from langgraph import AgentBuilder
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
load_dotenv()
|
||||
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
if not OPENAI_API_KEY:
|
||||
raise ValueError("OPENAI_API_KEY not set in environment")
|
||||
|
||||
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
|
||||
|
||||
builder = AgentBuilder(llm=llm)
|
||||
agent = builder.build()
|
||||
|
||||
def format_message(message) -> str:
|
||||
if message.content:
|
||||
return message.content
|
||||
if message.tool_calls:
|
||||
tool = message.tool_calls[0]
|
||||
return f"{tool['name']}({tool['args']})"
|
||||
return ""
|
||||
|
||||
step = 1
|
||||
|
||||
def format_chunk_message(chunk):
|
||||
global step
|
||||
message, meta = chunk
|
||||
if meta.get("langgraph_step") != step:
|
||||
step = meta.get("langgraph_step")
|
||||
print("\n --- --- --- \n")
|
||||
if message.content:
|
||||
print(message.content, end="", flush=True)
|
||||
|
||||
def main():
|
||||
user_input = input("Enter your question: ")
|
||||
stream = agent.stream(
|
||||
{"messages": [{"role": "human", "content": user_input}]},
|
||||
stream_mode=["messages", "updates"]
|
||||
)
|
||||
for chunk_type, chunk_data in stream:
|
||||
if chunk_type == "messages":
|
||||
format_chunk_message(chunk_data)
|
||||
elif chunk_type == "updates":
|
||||
if chunk_data.get("model"):
|
||||
last_message = chunk_data["model"]["messages"][-1]
|
||||
print(format_message(last_message))
|
||||
print("\n\nDone.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user