-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixes to the langgraph example agent (#4771)
* Fixes to the langgraph agent * Style fixes
- Loading branch information
Showing
10 changed files
with
53 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 17 additions & 47 deletions
64
examples/agent_framework_comparison/langgraph/analyze_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,39 @@ | ||
import json | ||
import os | ||
import sys | ||
|
||
sys.path.insert(1, os.path.join(sys.path[0], "..")) | ||
|
||
from dotenv import load_dotenv | ||
from langchain_core.messages import HumanMessage, SystemMessage | ||
from langchain_core.tools import tool | ||
from openai import OpenAI | ||
from langchain_openai import ChatOpenAI | ||
from openinference.instrumentation import using_prompt_template | ||
from openinference.semconv.trace import SpanAttributes | ||
from opentelemetry import trace | ||
from prompt_templates.data_analysis_template import PROMPT_TEMPLATE, SYSTEM_PROMPT | ||
|
||
load_dotenv() | ||
|
||
|
||
@tool | ||
def data_analyzer(args): | ||
def data_analyzer(original_prompt: str, data: str): | ||
"""Provides insights, trends, or analysis based on the data and prompt. | ||
Args: | ||
args (dict): A dictionary containing the data to analyze and the original user prompt | ||
that the data is based on. | ||
original_prompt (str): The original user prompt that the data is based on. | ||
data (str): The data to analyze. | ||
Returns: | ||
str: The analysis result. | ||
""" | ||
|
||
if isinstance(args, dict) and "prompt" in args and "data" in args: | ||
prompt = args["prompt"] | ||
data = args["data"] | ||
elif isinstance(args, str): | ||
try: | ||
args = json.loads(args) | ||
prompt = args["prompt"].strip() | ||
data = args["data"].strip() | ||
except ValueError: | ||
return "Invalid input: expected a dictionary with 'prompt' and 'data' keys or a string." | ||
else: | ||
return "Invalid input: expected a dictionary with 'prompt' and 'data' keys or a string." | ||
|
||
client = OpenAI() | ||
|
||
tracer = trace.get_tracer(__name__) | ||
with tracer.start_as_current_span("data_analysis_tool") as span: | ||
span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "CHAIN") | ||
span.set_attribute( | ||
SpanAttributes.INPUT_VALUE, | ||
PROMPT_TEMPLATE.format(PROMPT=prompt, DATA=data), | ||
) | ||
with using_prompt_template( | ||
template=PROMPT_TEMPLATE, | ||
variables={"PROMPT": prompt, "DATA": data}, | ||
version="v0.1", | ||
): | ||
response = client.chat.completions.create( | ||
model="gpt-4o", | ||
messages=[ | ||
{"role": "system", "content": SYSTEM_PROMPT}, | ||
{ | ||
"role": "user", | ||
"content": PROMPT_TEMPLATE.format(PROMPT=prompt, DATA=data), | ||
}, | ||
], | ||
) | ||
analysis_result = response.choices[0].message.content | ||
span.set_attribute(SpanAttributes.OUTPUT_VALUE, analysis_result) | ||
return analysis_result | ||
with using_prompt_template( | ||
template=PROMPT_TEMPLATE, | ||
variables={"PROMPT": original_prompt, "DATA": data}, | ||
version="v0.1", | ||
): | ||
model = ChatOpenAI(model="gpt-4o") | ||
messages = [ | ||
SystemMessage(content=SYSTEM_PROMPT), | ||
HumanMessage(content=PROMPT_TEMPLATE.format(PROMPT=original_prompt, DATA=data)), | ||
] | ||
response = model.invoke(messages) | ||
return response.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
examples/agent_framework_comparison/prompt_templates/router_template.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters