问题
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.agents import tool
from langchain.memory import ConversationBufferMemory
from langchain_community.llms.ollama import Ollama
from langchain.pydantic_v1 import BaseModel, Field
memory = ConversationBufferMemory()
llm = Ollama(model="llama2")
@tool
def get_word_length(word: str) -> int:
"""Returns the length of a word."""
return len(word)
@tool
def sum_two_number(a: int, b: int) -> int:
"""Returns the sum of two numbers."""
print(f"输入的参数是{a},{b}")
return a+b
class ReleaseInfo(BaseModel):
release_date: str = Field(description="should be the date of the release happen, should be formatted as YYYY-MM-DD")
release_content: str = Field(description="should be the content of the release")
qa: list = Field(description="should be the qa of the release")
be: list = Field(description="should be the backend developer of the release")
fe: list = Field(description="should be the frontend developer of the release")
services: list = Field(description="should be the services of the release")
@tool("release_notification", args_schema=ReleaseInfo, return_direct=True)
def release_notification(release_info: ReleaseInfo):
"""Call the api to send a notification of the release info"""
print(f"notify: {release_info}")
tools = [
Tool.from_function(
name="GET_WORD_LENGTH",
func=get_word_length,
description="this tool require you to provide a word, and return the length of the word. "
),
Tool.from_function(
name="SUM_TWO_NUMBERS",
func=sum_two_number,
description="this tool require you to provide two number, and return the sum of the two number. "
),
Tool.from_function(
name="RELEASE_NOTIFICATION",
func=release_notification,
description="""this tool require you to provide release info and call the api to send a notification
release info could be like this:
```
3/14 Release
Remove Market consent
QA:@Mandy x(QA UTC+8)
FE:@Sans X+8
BE:@robin w
Services:accounts-ui、pwa-ui、could-account
```
then we can know the release_date is 2024-03-14, release content is Remove Market consent,
qa is [Mandy x(QA UTC+8)] , fe is [Sans X+8] , be is [robin w], services are [accounts-ui,pwa-ui,could-account]
"""
),
]
SUFFIX = """Provide the final answer and terminate the chain of thought once you have an answer. Begin!
Question: {input}
Thought:{agent_scratchpad}"""
agent = initialize_agent(
tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True,
handle_parsing_errors=True, max_iterations=5,memory=memory, agent_kwargs={'suffix':SUFFIX})
if __name__ == '__main__':
# agent.run("What is the inventory of sneakers?")
while True:
human_input = input("Human: ")
response = agent.invoke(human_input)
print(f"Assistant: {response}")
当我传入input:
3/13 Release
FIX some bug
QA: @zeven
BE: @ted w
BE Services: capital,capital-job
报错信息
/Users/user/anaconda3/envs/AI/bin/python -X pycache_prefix=/Users/user/Library/Caches/JetBrains/PyCharm2023.3/cpython-cache /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 59257 --file /Users/user/Documents/Learn/AI/demo/main2.py
Connected to pydev debugger (build 233.14475.56)
/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function initialize_agent
was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.
warn_deprecated(
Human: 3/13 Release
Capital add BCH network
QA: @zeven 【utc+8】
BE: @ted w
BE Services: capital,capital-job
Entering new AgentExecutor chain…
Thought: Do I need to use a tool? Yes
Action: RELEASE_NOTIFICATION
Action Input: release info as follows:
3/13 Release
Remove Market consent
QA:@Mandy x(QA UTC+8)
FE:@Sans X+8
BE:@robin w
Services:accounts-ui、pwa-ui、could-account
```python-BaseException
Traceback (most recent call last):
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain/chains/base.py", line 163, in invoke
raise e
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain/chains/base.py", line 153, in invoke
self._call(inputs, run_manager=run_manager)
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain/agents/agent.py", line 1432, in _call
next_step_output = self._take_next_step(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain/agents/agent.py", line 1138, in _take_next_step
[
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain/agents/agent.py", line 1138, in <listcomp>
[
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain/agents/agent.py", line 1223, in _iter_next_step
yield self._perform_agent_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain/agents/agent.py", line 1245, in _perform_agent_action
observation = tool.run(
^^^^^^^^^
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/tools.py", line 382, in run
raise e
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/tools.py", line 376, in run
self._run(*tool_args, run_manager=run_manager, **tool_kwargs)
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/tools.py", line 574, in _run
self.func(
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/tools.py", line 515, in __call__
return self.run(tool_input, callbacks=callbacks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/tools.py", line 382, in run
raise e
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/tools.py", line 373, in run
parsed_input = self._parse_input(tool_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/langchain_core/tools.py", line 276, in _parse_input
input_args.validate({key_: tool_input})
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/pydantic/v1/main.py", line 711, in validate
return cls(**value)
^^^^^^^^^^^^
File "/Users/user/anaconda3/envs/AI/lib/python3.11/site-packages/pydantic/v1/main.py", line 341, in __init__
raise validation_error
pydantic.v1.error_wrappers.ValidationError: 5 validation errors for ReleaseInfo
release_content
field required (type=value_error.missing)
qa
field required (type=value_error.missing)
be
field required (type=value_error.missing)
fe
field required (type=value_error.missing)
services
field required (type=value_error.missing)
Process finished with exit code 1
### 环境
本地运行了Ollama,模型选择的是llama2:7b