[Python] Claude Does not Call Query OpenSearch through Bedrock

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    Here's my function to invoke Claude to answer context relevant question. I have created embeddings and stored in the OpenSearch database with separate functions. After running this code

    def query_with_context(query_text, top_k=3):
    """
    Query OpenSearch for similar documents and use Claude to generate an answer

    Args:
    query_text: The user's question
    top_k: Number of similar documents to retrieve
    """
    # Get query embedding from Titan
    query_embedding = get_titan_embeddings(query_text)

    # Connect to OpenSearch
    client = OpenSearch(
    hosts=[{'host': 'domain name', 'port': 443}],
    http_auth=auth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection
    )

    # Perform vector similarity search
    search_query = {
    "size": top_k,
    "query": {
    "script_score": {
    "query": {"match_all": {}},
    "script": {
    "source": "knn_score",
    "params": {
    "field": "vector",
    "query_value": query_embedding,
    "space_type": "cosinesimil"
    }
    }
    }
    }
    }

    # Get similar documents
    response = client.search(
    index="text-embeddings",
    body=search_query
    )

    # Extract relevant contexts
    contexts = []
    for hit in response['hits']['hits']:
    contexts.append(hit['_source']['text'])

    # Combine contexts
    combined_context = "\n\n".join(contexts)

    # Initialize Bedrock client for Claude
    bedrock_runtime = boto3.client('bedrock-runtime', region_name='us-east-1')

    # Prepare prompt with context
    prompt = f"""Given the following context:
    {combined_context}

    Please answer this question: {query_text}

    Use only the information from the provided context. If you cannot answer the question based on the context, say so."""

    # Query Claude with context
    claude_response = bedrock_runtime.invoke_model(
    modelId='anthropic.claude-3-7-sonnet-20250219-v1:0',
    body=json.dumps({
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 200,
    "messages": [
    {
    "role": "user",
    "content": prompt
    }
    ]
    }),
    accept='application/json',
    contentType='application/json'
    )

    # Parse Claude's response
    response_body = json.loads(claude_response.get('body').read())
    answer = response_body['content'][0]['text']

    return {
    "answer": answer,
    "contexts": contexts
    }


    I get the following error code

    opensearchpy.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'compile error')


    Any suggestions what it might be related to? Incorrect indexing? Some other issue? I am a bit lost here.

    Continue reading...

Compartilhe esta Página