Skip to content

Error in endpoint access for model lama2

0
from langchain import LLMChain
from langchain import SagemakerEndpoint
from langchain.prompts import PromptTemplate
from langchain.llms.sagemaker_endpoint import LLMContentHandler
import os
import json

endpoint_name = os.environ["LLAMA_2_ENDPOINT"]
region = os.environ["AWS_REGION"]

class ContentHandler(LLMContentHandler):
    content_type = "application/json"
    accepts = "application/json"

    def transform_input(self, prompt: str, model_kwargs: dict) -> bytes:
        input_str = json.dumps({"inputs" : [[{"role" : "system",
        "content" : "You are a kind robot."},
        {"role" : "user", "content" : prompt}]],
        "parameters" : {**model_kwargs}})
        return input_str.encode('utf-8')
    
    def transform_output(self, output: bytes) -> str:
        response_json = json.loads(output.read().decode("utf-8"))
        return response_json[0]["generation"]["content"]

template = "{content}"

prompt = PromptTemplate.from_template(template)

content_handler = ContentHandler()

llm=SagemakerEndpoint(
     endpoint_name=endpoint_name, 
     region_name=region, 
     model_kwargs={"max_new_tokens": 700, "top_p": 0.9, "temperature": 0.6},
     endpoint_kwargs={"CustomAttributes": 'accept_eula=true'},
     content_handler=content_handler
 )


llm_chain = LLMChain(
     llm=llm,
     prompt=prompt
 )

llm_chain.run({"How can I travel from New York to Los Angeles?"})

For this code i am getting error ValueError: Error raised by inference endpoint: An error occurred (ValidationError) when calling the InvokeEndpoint operation: Inference Component Name header is required for endpoints to which you plan to deploy inference components. Please include Inference Component Name header or consider using SageMaker models. Please help me to solve this error

asked 3 years ago1.4K views

1 Answer
1

The error you're encountering, ValueError: Error raised by inference endpoint: An error occurred (ValidationError) when calling the InvokeEndpoint operation: Inference Component Name header is required for endpoints to which you plan to deploy inference components, suggests that the request made to the SageMaker endpoint is missing a required header. This header is the Inference Component Name which is necessary for invoking the endpoint, especially when dealing with inference components.

The issue seems to be with how the request is being made to the SageMaker endpoint. To resolve this error, you need to include the Inference Component Name header in your endpoint invocation request. You need to add the Inference Component Name header to the endpoint_kwargs. Since you are already using CustomAttributes, you can append this additional header to it. llm = SagemakerEndpoint( endpoint_name=endpoint_name, region_name=region, model_kwargs={"max_new_tokens": 700, "top_p": 0.9, "temperature": 0.6}, endpoint_kwargs={ "CustomAttributes": 'accept_eula=true,InferenceComponentName=MyInferenceComponent' }, content_handler=content_handler )

answered 3 years ago

  • I have tried

    llm = SagemakerEndpoint(
        endpoint_name=endpoint_name,
        region_name=region, 
        model_kwargs={"max_new_tokens": 700, "top_p": 0.9, "temperature": 0.6}, 
        endpoint_kwargs={ "CustomAttributes": f'accept_eula=true,InferenceComponentName={MyInferenceComponent}'}, 
        content_handler=content_handler )
    

    and also as you suggested

    llm = SagemakerEndpoint(
        endpoint_name=endpoint_name,
        region_name=region, 
        model_kwargs={"max_new_tokens": 700, "top_p": 0.9, "temperature": 0.6}, 
        endpoint_kwargs={ "CustomAttributes": 'accept_eula=true,InferenceComponentName=MyInferenceComponent'}, 
        content_handler=content_handler )
    

    both the case's i am getting same error

    ValueError: Error raised by inference endpoint: An error occurred (ValidationError) when calling the InvokeEndpoint operation: Inference Component Name header is required for endpoints to which you plan to deploy inference components. Please include Inference Component Name header or consider using SageMaker models.

  • @Dipika I found that I had to pass the inference component name directly to endpoint args, rather than as part of custom attributes, like the following:

    endpoint_kwargs={"InferenceComponentName":'<my-inference-component-name>'},
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.