Skip to content

Instantly share code, notes, and snippets.

@hustshawn
Last active March 22, 2024 15:18
Show Gist options
  • Save hustshawn/80810688b8c3b3ad5dd39da378f6ce7c to your computer and use it in GitHub Desktop.
Save hustshawn/80810688b8c3b3ad5dd39da378f6ce7c to your computer and use it in GitHub Desktop.

Revisions

  1. hustshawn revised this gist Feb 3, 2024. 1 changed file with 64 additions and 0 deletions.
    64 changes: 64 additions & 0 deletions search_math_agent.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    from langchain import hub
    from langchain.agents import AgentExecutor, load_tools
    from langchain.agents.format_scratchpad import format_log_to_str
    from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
    from langchain.tools.render import render_text_description
    from langchain_community.llms.bedrock import Bedrock
    from langchain_community.chat_models.bedrock import BedrockChat

    import dotenv
    dotenv.load_dotenv()


    llm = Bedrock(
    credentials_profile_name='us-east-1',
    model_id='anthropic.claude-v2:1',
    )
    chat_model = BedrockChat(
    credentials_profile_name='us-east-1',
    model_id='anthropic.claude-v2:1'
    )


    tools = load_tools(["serpapi", "llm-math"], llm=llm)

    prompt = hub.pull("hwchase17/react-json")
    prompt = prompt.partial(
    tools=render_text_description(tools),
    tool_names=", ".join([t.name for t in tools]),
    )


    chat_model_with_stop = chat_model.bind(
    stop=["\n\nHuman:"]
    )
    agent = (
    {
    "input": lambda x: x["input"],
    "agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),
    }
    | prompt
    | chat_model_with_stop
    | ReActJsonSingleInputOutputParser()
    )

    agent_executor = AgentExecutor(
    agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)

    agent_executor.invoke(
    {
    "input": "how much is 27*313*289?"
    }
    # {
    # "input": "Which city has a larger population, Guiyang or Tacheng?"
    # }
    # {
    # "input": "what's the smallest prime number greater than 1000?"
    # }
    # {
    # "input": "The Japanese military officer Kenji Hatanaka conspired to prevent the broadcast of the surrender of this emperor who passed away a few decades later and was succeeded by whom?"
    # }
    # {
    # "input": "who is the F1 World Champion in 2023? what's his age raised to the 0.43 power?"
    # }
    )
  2. hustshawn renamed this gist Feb 3, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. hustshawn revised this gist Feb 3, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion search_math_agent.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@
    from langchain.tools.render import render_text_description
    from langchain_community.llms.bedrock import Bedrock
    from langchain_community.chat_models.bedrock import BedrockChat
    from langchain_community.chat_models.bedrock import BedrockChat

    import dotenv
    dotenv.load_dotenv()
  4. hustshawn revised this gist Feb 3, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions search_math_agent.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    from langchain.tools.render import render_text_description
    from langchain_community.llms.bedrock import Bedrock
    from langchain_community.chat_models.bedrock import BedrockChat
    from langchain_community.chat_models.bedrock import BedrockChat

    import dotenv
    dotenv.load_dotenv()
  5. hustshawn revised this gist Feb 3, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions search_math_agent.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    from langchain import hub
    from langchain.agents import AgentExecutor, load_tools
    from langchain.agents.format_scratchpad import format_log_to_str
    from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
    from langchain.tools.render import render_text_description
    from langchain import hub
    from langchain_community.llms.bedrock import Bedrock
    from langchain.chat_models.bedrock import BedrockChat
    from langchain_community.chat_models.bedrock import BedrockChat

    import dotenv
    dotenv.load_dotenv()
  6. hustshawn revised this gist Feb 3, 2024. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions search_math_agent.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,8 @@
    from langchain.agents import AgentExecutor, load_tools
    from langchain.agents.format_scratchpad import format_log_to_str
    from langchain.agents import AgentExecutor
    from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
    from langchain.tools.render import render_text_description
    from langchain import hub
    from langchain.agents import load_tools
    from langchain_community.llms.bedrock import Bedrock
    from langchain.chat_models.bedrock import BedrockChat

  7. hustshawn revised this gist Feb 3, 2024. No changes.
  8. hustshawn revised this gist Feb 3, 2024. 1 changed file with 99 additions and 0 deletions.
    99 changes: 99 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    aiohttp==3.9.2
    aiosignal==1.3.1
    annotated-types==0.6.0
    anyio==4.2.0
    appnope==0.1.3
    asttokens==2.4.1
    attrs==23.2.0
    boto3==1.34.34
    botocore==1.34.34
    certifi==2023.11.17
    charset-normalizer==3.3.2
    click==8.1.7
    comm==0.2.1
    dataclasses-json==0.6.3
    debugpy==1.8.0
    decorator==5.1.1
    Deprecated==1.2.14
    distro==1.9.0
    executing==2.0.1
    filelock==3.13.1
    frozenlist==1.4.1
    fsspec==2023.12.2
    google_search_results==2.4.2
    greenlet==3.0.3
    h11==0.14.0
    httpcore==1.0.2
    httpx==0.26.0
    huggingface-hub==0.20.3
    idna==3.6
    ipykernel==6.29.0
    ipython==8.20.0
    jedi==0.19.1
    Jinja2==3.1.3
    jmespath==1.0.1
    joblib==1.3.2
    jsonpatch==1.33
    jsonpointer==2.4
    jupyter_client==8.6.0
    jupyter_core==5.7.1
    langchain==0.1.5
    langchain-community==0.0.17
    langchain-core==0.1.18
    langchain-openai==0.0.5
    langchainhub==0.1.14
    langgraph==0.0.21
    langsmith==0.0.86
    llama-index==0.9.39
    MarkupSafe==2.1.5
    marshmallow==3.20.2
    matplotlib-inline==0.1.6
    multidict==6.0.4
    mypy-extensions==1.0.0
    nest-asyncio==1.6.0
    networkx==3.2.1
    nltk==3.8.1
    numexpr==2.9.0
    numpy==1.26.3
    openai==1.10.0
    packaging==23.2
    pandas==2.2.0
    parso==0.8.3
    pexpect==4.9.0
    platformdirs==4.1.0
    prompt-toolkit==3.0.43
    psutil==5.9.8
    ptyprocess==0.7.0
    pure-eval==0.2.2
    pydantic==2.5.3
    pydantic_core==2.14.6
    Pygments==2.17.2
    python-dateutil==2.8.2
    python-dotenv==1.0.1
    pytz==2023.4
    PyYAML==6.0.1
    pyzmq==25.1.2
    regex==2023.12.25
    requests==2.31.0
    s3transfer==0.10.0
    safetensors==0.4.2
    six==1.16.0
    sniffio==1.3.0
    SQLAlchemy==2.0.25
    stack-data==0.6.3
    tavily-python==0.3.1
    tenacity==8.2.3
    tiktoken==0.5.2
    tokenizers==0.15.1
    tornado==6.4
    tqdm==4.66.1
    traitlets==5.14.1
    transformers==4.37.2
    types-requests==2.31.0.20240125
    typing-inspect==0.9.0
    typing_extensions==4.9.0
    tzdata==2023.4
    urllib3==2.0.7
    wcwidth==0.2.13
    wrapt==1.16.0
    yarl==1.9.4
  9. hustshawn revised this gist Feb 3, 2024. 1 changed file with 63 additions and 1 deletion.
    64 changes: 63 additions & 1 deletion search_math_agent.py
    Original file line number Diff line number Diff line change
    @@ -1 +1,63 @@
    ‎‎​
    from langchain.agents import AgentExecutor, load_tools
    from langchain.agents.format_scratchpad import format_log_to_str
    from langchain.agents import AgentExecutor
    from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
    from langchain.tools.render import render_text_description
    from langchain import hub
    from langchain.agents import load_tools
    from langchain_community.llms.bedrock import Bedrock
    from langchain.chat_models.bedrock import BedrockChat

    import dotenv
    dotenv.load_dotenv()


    llm = Bedrock(
    credentials_profile_name='us-east-1',
    model_id='anthropic.claude-v2:1',
    )
    chat_model = BedrockChat(
    credentials_profile_name='us-east-1',
    model_id='anthropic.claude-v2:1'
    )


    tools = load_tools(["serpapi", "llm-math"], llm=llm)

    prompt = hub.pull("hwchase17/react-json")
    prompt = prompt.partial(
    tools=render_text_description(tools),
    tool_names=", ".join([t.name for t in tools]),
    )


    chat_model_with_stop = chat_model.bind(
    stop=["\n\nHuman:"]
    )
    agent = (
    {
    "input": lambda x: x["input"],
    "agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),
    }
    | prompt
    | chat_model_with_stop
    | ReActJsonSingleInputOutputParser()
    )

    agent_executor = AgentExecutor(
    agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)

    agent_executor.invoke(
    {
    "input": "Which city has a larger population, Guiyang or Tacheng?"
    }
    # {
    # "input": "what's the smallest prime number greater than 1000?"
    # }
    # {
    # "input": "The Japanese military officer Kenji Hatanaka conspired to prevent the broadcast of the surrender of this emperor who passed away a few decades later and was succeeded by whom?"
    # }
    # {
    # "input": "who is the F1 World Champion in 2023? what's his age raised to the 0.43 power?"
    # }
    )
  10. hustshawn created this gist Feb 3, 2024.
    1 change: 1 addition & 0 deletions search_math_agent.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​