mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
refactor BedrockEmbeddings class (#7266)
#### Description refactor BedrockEmbeddings class to clean code as below: 1. inline content type and accept 2. rewrite input_body as a dictionary literal 3. no need to declare embeddings variable, so remove it
This commit is contained in:
parent
c7cf11b8ab
commit
8c371e12eb
@ -107,27 +107,21 @@ class BedrockEmbeddings(BaseModel, Embeddings):
|
||||
text = text.replace(os.linesep, " ")
|
||||
_model_kwargs = self.model_kwargs or {}
|
||||
|
||||
input_body = {**_model_kwargs}
|
||||
input_body["inputText"] = text
|
||||
input_body = {**_model_kwargs, "inputText": text}
|
||||
body = json.dumps(input_body)
|
||||
content_type = "application/json"
|
||||
accepts = "application/json"
|
||||
|
||||
embeddings = []
|
||||
try:
|
||||
response = self.client.invoke_model(
|
||||
body=body,
|
||||
modelId=self.model_id,
|
||||
accept=accepts,
|
||||
contentType=content_type,
|
||||
accept="application/json",
|
||||
contentType="application/json",
|
||||
)
|
||||
response_body = json.loads(response.get("body").read())
|
||||
embeddings = response_body.get("embedding")
|
||||
return response_body.get("embedding")
|
||||
except Exception as e:
|
||||
raise ValueError(f"Error raised by inference endpoint: {e}")
|
||||
|
||||
return embeddings
|
||||
|
||||
def embed_documents(
|
||||
self, texts: List[str], chunk_size: int = 1
|
||||
) -> List[List[float]]:
|
||||
|
Loading…
Reference in New Issue
Block a user