AI21 LLM is not Runnable when using via Langchain
Closed this issue · 3 comments
I am facing an issue with AI21 LLM, instance of LLM should be runnable.
I have used my AI21Client to get access my api_key and use my client
Then I have used Langchain agents to add serpapi in my system.
To fetch results from the google-search-results
from langchain.agents import AgentType
from langchain.agents import load_tools, initialize_agent
from langchain.llms import ai21
from ai21 import AI21Client
client = AI21Client(api_key=jurasic_key)
tool = load_tools(["serpapi"],serpapi_api_key=serpapi_key,llm=client)
agent = initialize_agent(tool,client,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,verbose=True)
Output for:
client -> <ai21.clients.studio.ai21_client.AI21Client at 0x1e3e2e34250>
tool -> [Tool(name='Search', description='A search engine. Useful for when you need to answer questions about current events. Input should be a search query.', func=<bound method SerpAPIWrapper.run of SerpAPIWrapper(search_engine=<class 'serpapi.google_search.GoogleSearch'>, params={'engine': 'google', 'google_domain': 'google.com', 'gl': 'us', 'hl': 'en'}, serpapi_api_key='serpapi_key', aiosession=None)>, coroutine=<bound method SerpAPIWrapper.arun of SerpAPIWrapper(search_engine=<class 'serpapi.google_search.GoogleSearch'>, params={'engine': 'google', 'google_domain': 'google.com', 'gl': 'us', 'hl': 'en'}, serpapi_api_key='serpapi_key', aiosession=None)>)
For agents it should have considered my tool and client but it is throwing this error
Error:
ValidationError Traceback (most recent call last)
Cell In[103], line 1
----> 1 agent = initialize_agent(tool,client,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,verbose=True)
2 agent
File u:\GenAI.venv\lib\site-packages\langchain_core_api\deprecation.py:168, in deprecated..deprecate..warning_emitting_wrapper(*args, **kwargs)
166 warned = True
167 emit_warning()
--> 168 return wrapped(*args, **kwargs)
File u:\GenAI.venv\lib\site-packages\langchain\agents\initialize.py:67, in initialize_agent(tools, llm, agent, callback_manager, agent_path, agent_kwargs, tags, **kwargs)
65 agent_cls = AGENT_TO_CLASS[agent]
66 agent_kwargs = agent_kwargs or {}
---> 67 agent_obj = agent_cls.from_llm_and_tools(
68 llm, tools, callback_manager=callback_manager, **agent_kwargs
69 )
70 elif agent_path is not None:
71 agent_obj = load_agent(
72 agent_path, llm=llm, tools=tools, callback_manager=callback_manager
73 )
File u:\GenAI.venv\lib\site-packages\langchain\agents\mrkl\base.py:113, in ZeroShotAgent.from_llm_and_tools(cls, llm, tools, callback_manager, output_parser, prefix, suffix, format_instructions, input_variables, **kwargs)
105 cls._validate_tools(tools)
106 prompt = cls.create_prompt(
...
ValidationError: 2 validation errors for LLMChain
llm
instance of Runnable expected (type=type_error.arbitrary_type; expected_arbitrary_type=Runnable)
llm
instance of Runnable expected (type=type_error.arbitrary_type; expected_arbitrary_type=Runnable)
How to make my LLM instance runnable ?
AI21Client is not an LLM and is not a runnable.
The way you should use AI21's models is as follows:
from langchain.agents import AgentType
from langchain.agents import load_tools, initialize_agent
from langchain_ai21 import AI21LLM
llm = AI21LLM(model="j2-ultra", api_key="Replace with AI21's API_KEY")
tool = load_tools(["serpapi"],serpapi_api_key="Replace with SerpAPI's API_KEY",llm=llm)
agent = initialize_agent(tool,llm,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,verbose=True)
@utkarsh-iitbhu You can also use our new chat model, Jamba, via the ChatAI21
class as follows -
from langchain.agents import AgentType
from langchain.agents import initialize_agent
from langchain_ai21 import ChatAI21
from langchain_community.agent_toolkits.load_tools import load_tools
llm = ChatAI21(model="jamba-instruct", api_key="Replace with AI21's API_KEY")
tool = load_tools(["serpapi"], serpapi_api_key="Replace with SerpAPI's API_KEY", llm=llm)
agent = initialize_agent(tool, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True,
handle_parsing_errors=True)
agent.invoke("Who are AI21 Labs?")
You can also use the following prompt engineering to avoid some langchain warnings and receive a cleaner output
from langchain.agents import AgentType
from langchain.agents import initialize_agent
from langchain_ai21 import ChatAI21
from langchain_core.prompts import PromptTemplate
from langchain_community.agent_toolkits.load_tools import load_tools
template = """
You are a great AI-Assistant that has access to additional tools in order to answer the following questions as best you can. Always answer in the same language as the user question. You have access to the following tools:
{tools}
To use a tool, please use the following format:
'''
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat 3 times)
'''
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
'''
Thought: Do I need to use a tool? No
Final Answer: [your response here]
'''
Begin!
Question: {input}
Thought:{agent_scratchpad}
"""
prompt = PromptTemplate.from_template(template)
llm = ChatAI21(model="jamba-instruct", api_key="Replace with AI21's API_KEY")
tool = load_tools(["serpapi"], serpapi_api_key="Replace with SerpAPI's API_KEY", llm=llm)
agent = initialize_agent(tool, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True,
handle_parsing_errors=True, prompt=prompt)
agent.invoke("Who are AI21 Labs?")
Hope this helps! :)
Thank you, sir; now I can run my agents !!!