langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
91.37k stars 14.54k forks source link

pydantic.error_wrappers.ValidationError: 2 validation errors for LLMChain #20192

Closed AnjaliKeshri closed 1 month ago

AnjaliKeshri commented 4 months ago

Checked other resources

Example Code

import os; from langchain.llms.openai import OpenAI from openai import AzureOpenAI from dotenv import load_dotenv from langchain.chains.api.base import LLMChain from langchain.chains.api.base import APIChain from langchain.chains.api import open_meteo_docs from langchain_core.prompts import PromptTemplate load_dotenv()

client = AzureOpenAI( api_key = os.getenv("AZURE_OPENAI_API_KEY"),
api_version = "2024-02-15-preview", azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") ) prompt = PromptTemplate( input_variables=["api_url"], template="""Act as a technical writer. Write detailed documentation for the API that exists at {api_url}. Only detail the request, do not describe the response. Do not include any parameters not in the sample endpoint.""" )

chain = LLMChain( llm=client, verbose=True, prompt=prompt ) url = "https://pitchfork.com/api/v2/search/?genre=experimental&genre=global&genre=jazz&genre=metal&genre=pop&genre=rap&genre=rock&types=reviews&sort=publishdate%20desc%2Cposition%20asc&size=5&start=0&rating_from=0.0"

response = chain.run(url) print(response)

Error Message and Stack Trace (if applicable)

super().__init__(**kwargs)

File "pydantic\main.py", line 341, in pydantic.main.BaseModel.init pydantic.error_wrappers.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)

Description

I am trying to call an external api. Here i am using langchain version==0.1.0 pydantic==1.10.8 and openai==1.7.0 I am using azureopenai instead openai.. and whenever i want to use the LLMChain , i got this 2 validation error. I am confused that whether this is because of any version issue or the implementation of the code.

System Info

Python 3.10.11 langchain version==0.1.0 pydantic==1.10.8 openai==1.7.0 openapi-schema-pydantic==1.2.4

spike-spiegel-21 commented 4 months ago

In command line: pip install langchain-openai Instead of using from openai import AzureOpenAI use from langchain_openai import AzureOpenAI

AnjaliKeshri commented 4 months ago

This is my modified code

import os; from langchain_openai import AzureOpenAI from dotenv import load_dotenv from langchain_core.prompts import PromptTemplate load_dotenv()

client = AzureOpenAI( api_key = os.getenv("AZURE_OPENAI_API_KEY"),
api_version = "2024-02-15-preview", azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") ) prompt = PromptTemplate( input_variables=["api_url"], template="""Act as a technical writer. Write detailed documentation for the API that exists at {api_url}. Only detail the request, do not describe the response. Do not include any parameters not in the sample endpoint.""" )

url = "https://api.open-meteo.com/v1/forecast?latitude=48.1351&longitude=11.5820&hourly=temperature_2m&temperature_unit=fahrenheit&current_weather=true"

chain = prompt | client

response = chain.invoke({"api_url": url}) print(response)

but now i am getting this error

File "C:\Users\2282635\OneDrive - Cognizant\Desktop\Chatbot\Langchain\test-azure.py", line 25, in response = chain.invoke({"api_url": url}) File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\runnables\base.py", line 2499, in invoke input = step.invoke( File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\language_models\llms.py", line 248, in invoke self.generate_prompt( File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\language_models\llms.py", line 569, in generate_prompt return self.generate(prompt_strings, stop=stop, callbacks=callbacks, kwargs) File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\language_models\llms.py", line 748, in generate output = self._generate_helper( File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\language_models\llms.py", line 606, in _generate_helper raise e File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_core\language_models\llms.py", line 593, in _generate_helper self._generate( File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain_openai\llms\base.py", line 368, in _generate response = self.client.create(prompt=_prompts, params) File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\openai_utils_utils.py", line 275, in wrapper return func(*args, **kwargs) File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\resources\completions.py", line 516, in create return self._post( File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\openai_base_client.py", line 1213, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\openai_base_client.py", line 902, in request return self._request( File "C:\Users\2282635\AppData\Local\Programs\Python\Python310\lib\site-packages\openai_base_client.py", line 993, in _request raise self._make_status_error_from_response(err.response) from None openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

spike-spiegel-21 commented 4 months ago

check you azure_openai_endpoint or azure_openai_key