-
Checked other resources
Commit to Help
Example Codefrom langchain_experimental.agents import create_pandas_dataframe_agent
import os
import pandas as pd
agent = create_pandas_dataframe_agent(
llm,
df,
agent_type="openai-tools",
verbose=True,
return_intermediate_steps=True,
allow_dangerous_code=True,
allow_dangerous_requests=True,
)
agent.invoke({"input":query_str}) DescriptionAre there any good packages, such as DsPy or native LangChain packages, that can help tune a prompt before passing it into query_str in the example below? How can I narrate my output in natural language after the agent provides its output? My objective is to extract data from the agent and pass it to another agent that can narrate the results in human-readable language, similar to how llama_index handles response synthesis. Please help. System InfoName: langchain |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 17 replies
-
Yes, there are good packages such as LangChain that can help tune a prompt before passing it into from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
from typing import List, Optional
from langchain_core.pydantic_v1 import BaseModel, Field
class Search(BaseModel):
query: str = Field(
...,
description="Primary similarity search query applied to video transcripts.",
)
sub_queries: List[str] = Field(
default_factory=list, description="List of sub-questions derived from the main query."
)
publish_year: Optional[int] = Field(None, description="Year video was published")
system = """You are an expert at converting user questions into database queries. \
You have access to a database of tutorial videos about a software library for building LLM-powered applications. \
Given a question, return a list of database queries optimized to retrieve the most relevant results.
If there are acronyms or words you are not familiar with, do not try to rephrase them."""
prompt = ChatPromptTemplate.from_messages(
[
("system", system),
MessagesPlaceholder("examples", optional=True),
("human", "{question}"),
]
)
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(Search)
query_analyzer = {"question": RunnablePassthrough()} | prompt | structured_llm
# Adding examples to the prompt
examples = []
question = "What's chat langchain, is it a langchain template?"
query = Search(
query="What is chat langchain and is it a langchain template?",
sub_queries=["What is chat langchain", "What is a langchain template"],
)
examples.append({"input": question, "tool_calls": [query]})
question = "How to build multi-agent system and stream intermediate steps from it"
query = Search(
query="How to build multi-agent system and stream intermediate steps from it",
sub_queries=[
"How to build multi-agent system",
"How to stream intermediate steps from multi-agent system",
"How to stream intermediate steps",
],
)
examples.append({"input": question, "tool_calls": [query]})
question = "LangChain agents vs LangGraph?"
query = Search(
query="What's the difference between LangChain agents and LangGraph? How do you deploy them?",
sub_queries=[
"What are LangChain agents",
"What is LangGraph",
"How do you deploy LangChain agents",
"How do you deploy LangGraph",
],
)
examples.append({"input": question, "tool_calls": [query]})
# Helper function to convert examples to messages
import uuid
from typing import Dict
from langchain_core.messages import (
AIMessage,
BaseMessage,
HumanMessage,
ToolMessage,
)
def tool_example_to_messages(example: Dict) -> List[BaseMessage]:
messages: List[BaseMessage] = [HumanMessage(content=example["input"])]
openai_tool_calls = []
for tool_call in example["tool_calls"]:
openai_tool_calls.append(
{
"id": str(uuid.uuid4()),
"type": "function",
"function": {
"name": tool_call.__class__.__name__,
"arguments": tool_call.json(),
},
}
)
messages.append(
AIMessage(content="", additional_kwargs={"tool_calls": openai_tool_calls})
)
tool_outputs = example.get("tool_outputs") or [
"You have correctly called this tool."
] * len(openai_tool_calls)
for output, tool_call in zip(tool_outputs, openai_tool_calls):
messages.append(ToolMessage(content=output, tool_call_id=tool_call["id"]))
return messages
example_msgs = [msg for ex in examples for msg in tool_example_to_messages(ex)]
query_analyzer_with_examples = (
{"question": RunnablePassthrough()}
| prompt.partial(examples=example_msgs)
| structured_llm
)
# Using the query analyzer with examples
result = query_analyzer_with_examples.invoke(
"what's the difference between web voyager and reflection agents? do both use langgraph?"
) To narrate your output in natural language after the agent provides its output, you can simply format the result into a human-readable string. Here is an example: def narrate_output(result):
narration = f"The main query is: {result.query}\n"
if result.sub_queries:
narration += "The sub-queries are:\n"
for sub_query in result.sub_queries:
narration += f"- {sub_query}\n"
if result.publish_year:
narration += f"The video was published in the year: {result.publish_year}\n"
return narration
output_narration = narrate_output(result)
print(output_narration) This will provide a natural language narration of the output generated by the agent [1]. |
Beta Was this translation helpful? Give feedback.
-
@dosu.... this is great. One challenge I frequently encounter in my program is case sensitivity discrepancies between the attribute names in my pandas DataFrame and the way they are queried. For example, if my DataFrame column 'country' records 'USA', but the query uses 'US' or 'usa', the program returns "no data found." Is there a way to check the unique values in the DataFrame before writing these query snippets to assist in the query tuning process? I could convert all DataFrame values to lowercase and adjust the queries accordingly, but my problem is more complex. Sometimes, users input 'US' and expect feedback on 'USA'. Is there any strategy to mitigate this issue? |
Beta Was this translation helpful? Give feedback.
-
@dosu Can you help me in injecting above function in my final query please? I am new to Python as well as langchain...
|
Beta Was this translation helpful? Give feedback.
-
@dosu everything is working great.. Thank you! However, the only one thing is not working is |
Beta Was this translation helpful? Give feedback.
-
@dosu I am just looking to narrate the response that I got from my python agent. Can you help? |
Beta Was this translation helpful? Give feedback.
-
@dosu... One final question. The suggestion you provided is working excellently, and passing my query_str is yielding great results. However, I noticed that your BaseModel is primarily designed for YouTube transcripts, while I am mainly using a pandas DataFrame for agent queries. My question is: Do I need to modify the class search(BaseModel) and the system message accordingly? If so, could you please provide a similar snippet? The query you provided is below...
|
Beta Was this translation helpful? Give feedback.
To create embeddings for multiple columns (e.g., 'country_names' and 'city_names') in a DataFrame, incorporate those embeddings into a vector database, and call them for a query, you can follow these steps:
Here is the updated code: