Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prompt variable + langchain agent steps #86

Merged
merged 1 commit into from
Jul 3, 2024

Conversation

desaxce
Copy link
Contributor

@desaxce desaxce commented Jul 3, 2024

image

To test:

Start by creating a prompt template with name "LC Agent" from within Literal with the following messages:

  • system You are a helpful assistant
  • assistant {{chat_history}}
  • user {{input}}
  • assistant {{agent_scratchpad}}

Run the following:

# Set env vars:
# OPENAI_API_KEY
# TAVILY_API_KEY
# LITERAL_API_KEY
# LITERAL_API_URL

import os
import getpass

from literalai import LiteralClient

from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults

model = ChatOpenAI(model="gpt-4o")
search = TavilySearchResults(max_results=2)
tools = [search]

lai_client = LiteralClient(api_key=os.environ["LITERAL_API_KEY"], url=os.environ["LITERAL_API_URL"])
lai_prompt = lai_client.api.get_prompt(name="LC Agent")
prompt = lai_prompt.to_langchain_chat_prompt_template()


from langchain.agents import create_tool_calling_agent
from langchain.agents import AgentExecutor

agent = create_tool_calling_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)

lai_client.reset_context()
cb = lai_client.langchain_callback()

from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.runnables.config import RunnableConfig

agent_executor.invoke({
    "chat_history": [
            # You can specify the intermediary messages as tuples too.
            # ("human", "hi! my name is bob"),
            # ("ai", "Hello Bob! How can I assist you today?")
            HumanMessage(content="hi! my name is bob"),
            AIMessage(content="Hello Bob! How can I assist you today?"),
        ],
    "input": "whats the weather in sf?"
}, config=RunnableConfig(callbacks=[cb], run_name="Weather SF"))

Copy link
Contributor

@Dam-Buty Dam-Buty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@desaxce desaxce force-pushed the hugues/fix-langchain-agent branch from 0447ef5 to cd96375 Compare July 3, 2024 13:42
@desaxce desaxce merged commit 637a38e into main Jul 3, 2024
2 checks passed
@desaxce desaxce deleted the hugues/fix-langchain-agent branch July 3, 2024 13:52
@desaxce desaxce mentioned this pull request Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants