Yatan
Yatan

Reputation: 93

Langchain, call open AI

I am following quick start of Langchain to call open ai for LLM.

https://python.langchain.com/docs/get_started/quickstart

While running the below python code I am getting error.

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(openai_api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are world class technical documentation writer."),
    ("user", "{input}")
])

chain = prompt | llm 
chain.invoke({"input": "how can langsmith help with testing?"})
output_parser = StrOutputParser()
chain = prompt | llm | output_parser
chain.invoke({"input": "how can langsmith help with testing?"})

I am getting below error:

                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Python\Sysint_NPL2SQL\.venv\Lib\site-packages\openai\_base_client.py", line 902, in request
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Python\Sysint_NPL2SQL\.venv\Lib\site-packages\openai\_base_client.py", line 902, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "c:\Python\Sysint_NPL2SQL\.venv\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'}}
PS C:\Python\Sysint_NPL2SQL>  c:; cd 'c:\Python\Sysint_NPL2SQL'; & 'c:\Python\Sysint_NPL2SQL\.venv\Scripts\python.exe' 'c:\Users\yatanveer.singh\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '64396' '--' 'C:\Python\Sysint_NPL2SQL\langchaindemo.py'
PS C:\Python\Sysint_NPL2SQL>  c:; cd 'c:\Python\Sysint_NPL2SQL'; & 'c:\Python\Sysint_NPL2SQL\.venv\Scripts\python.exe' 'c:\Users\yatanveer.singh\.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '64413' '--' 'C:\Python\Sysint_NPL2SQL\langchaindemo.py'

Any pointer will help.

Thanks Yatan

Upvotes: 0

Views: 1065

Answers (1)

j3ffyang
j3ffyang

Reputation: 2470

Your code works in my env. The error you received

openai.NotFoundError: Error code: 404 

indicates your endpoint can't reach OpenAI's API

Upvotes: 0

Related Questions