langchain-ai / langchain

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

LocalProtocolError: Illegal header value b'Bearer ' #21261

Open andysingal opened 3 months ago

andysingal commented 3 months ago

Checked other resources

Example Code

!pip install langchain_openai langchain-core langchain-mistralai -qU

from typing import Optional

from langchain_core.pydantic_v1 import BaseModel, Field

class Person(BaseModel):
    """Information about a person."""

    # ^ Doc-string for the entity Person.
    # This doc-string is sent to the LLM as the description of the schema Person,
    # and it can help to improve extraction results.

    # Note that:
    # 1. Each field is an `optional` -- this allows the model to decline to extract it!
    # 2. Each field has a `description` -- this description is used by the LLM.
    # Having a good description can help improve extraction results.
    name: Optional[str] = Field(default=None, description="The name of the person")
    hair_color: Optional[str] = Field(
        default=None, description="The color of the peron's hair if known"
    )
    height_in_meters: Optional[str] = Field(
        default=None, description="Height measured in meters"
    )

from typing import Optional

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI

# Define a custom prompt to provide instructions and any additional context.
# 1) You can add examples into the prompt template to improve extraction quality
# 2) Introduce additional parameters to take context into account (e.g., include metadata
#    about the document from which the text was extracted.)
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are an expert extraction algorithm. "
            "Only extract relevant information from the text. "
            "If you do not know the value of an attribute asked to extract, "
            "return null for the attribute's value.",
        ),
        # Please see the how-to about improving performance with
        # reference examples.
        # MessagesPlaceholder('examples'),
        ("human", "{text}"),
    ]
)

from langchain_mistralai import ChatMistralAI

llm = ChatMistralAI(model="mistral-large-latest", temperature=0)

runnable = prompt | llm.with_structured_output(schema=Person)

text = "Alan Smith is 6 feet tall and has blond hair."
runnable.invoke({"text": text})

Error Message and Stack Trace (if applicable)

---------------------------------------------------------------------------
LocalProtocolError                        Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/httpx/_transports/default.py](https://localhost:8080/#) in map_httpcore_exceptions()
     68     try:
---> 69         yield
     70     except Exception as exc:

28 frames
LocalProtocolError: Illegal header value b'Bearer '

The above exception was the direct cause of the following exception:

LocalProtocolError                        Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/httpx/_transports/default.py](https://localhost:8080/#) in map_httpcore_exceptions()
     84 
     85         message = str(exc)
---> 86         raise mapped_exc(message) from exc
     87 
     88 

LocalProtocolError: Illegal header value b'Bearer '

Description

Trying https://python.langchain.com/docs/use_cases/extraction/quickstart/

System Info


System Information
------------------
> OS:  Linux
> OS Version:  #1 SMP PREEMPT_DYNAMIC Sat Nov 18 15:31:17 UTC 2023
> Python Version:  3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

Package Information
-------------------
> langchain_core: 0.1.50
> langsmith: 0.1.53
> langchain_mistralai: 0.1.6
> langchain_openai: 0.1.6

Packages not installed (Not Necessarily a Problem)
--------------------------------------------------
The following packages were not found:

> langgraph
> langserve
conditionedstimulus commented 3 months ago

You're missing the API key, and you'll need to read it using dotenv. If you're using Mistral, create the API key in your Mistral account. If you're using OpenAI, generate the API key there instead.

It's also part of the documentation:

# Set env vars for the relevant model or load from a .env file:
import dotenv
dotenv.load_dotenv()
andysingal commented 3 months ago

Thanks David , I guess I need to add my Mistral key in .env . Can you please update the docs too. Thanks Again!!

On Sat, May 4, 2024 at 00:04 David @.***> wrote:

You're missing the API key, and you'll need to read it using dotenv. If you're using Mistral, create the API key in your Mistral account. If you're using OpenAI, generate the API key there instead.

It's also part of the documentation:

Set env vars for the relevant model or load from a .env file:

import dotenv dotenv.load_dotenv()

— Reply to this email directly, view it on GitHub https://github.com/langchain-ai/langchain/issues/21261#issuecomment-2093553765, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE4LJNMY7GU5D3PXYZSMSA3ZAPKD7AVCNFSM6AAAAABHF4DHHCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJTGU2TGNZWGU . You are receiving this because you authored the thread.Message ID: @.***>

Daksh-Maru commented 1 month ago

I am also using Mistral Ai and Api key is being read, no issue with that but still I am receiving the same error after executing .invoke method.

MusanjeRichard5 commented 1 week ago

I am also using Mistral Api but am receiving the same error