langchain-ai / langchain

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

AmazonAPIGatewayEmbeddings class for text embeddings #11580

Closed brunopistone closed 7 months ago

brunopistone commented 11 months ago

Feature request

As of today, it's not possible to use Amazon API Gateway for exposing embeddings model and use it as part of a chain (e.g. ConversationalRetrievalChain). As of today, AmazonAPIGateway can be used only as LLM for text generation, but you cannot use it as embeddings for text embeddings generation (e.g. as part of ConversationalRetrievalChain)

Motivation

Amazon API Gateway can be adopted for both Text generation and Text Embeddings. Amazon Bedrock provides different type of models (LLMs and Embeddings models). In this way, developers can use Amazon API Gateway for Retrieval Augmented Generation solutions

Your contribution

The class can be defined as following:

from langchain.embeddings.bedrock import Embeddings
import requests
from typing import List

class AmazonAPIGatewayEmbeddings(Embeddings):
    def __init__(self, api_url, headers):
        self.api_url = api_url
        self.headers = headers

    def embed_documents(self, texts: List[str]) -> List[List[float]]:
        results = []
        for text in texts:
            response = requests.post(
                self.api_url,
                json={"inputs": text},
                headers=self.headers
            )
            results.append(response.json()[0]["embedding"])

        return results

    def embed_query(self, text: str) -> List[float]:
        response = requests.post(
                self.api_url,
                json={"inputs": text},
                headers=self.headers
            )

        return response.json()[0]["embedding"]

---

embeddings = AmazonAPIGatewayEmbeddings(
    api_url=f"{api_url}/invoke_model?model_id={model_id}",
    headers={
       ... # Required headers for the API invocation
    }
)

embeddings.embed_query("Hello, how are you?")
dosubot[bot] commented 7 months ago

Hi, @brunopistone

I'm helping the LangChain team manage their backlog and am marking this issue as stale. It looks like you requested the addition of an AmazonAPIGatewayEmbeddings class for text embeddings in the repository. The proposed class includes methods for embedding documents and queries using the API, and you provided a code example for implementing the class. As there hasn't been any further activity or comments on this issue, I wanted to check if this is still relevant to the latest version of the LangChain repository. If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your contribution, and we look forward to hearing from you.