mirror of https://github.com/hwchase17/langchain
community[minor]: Added GigaChat Embeddings support + updated previous GigaChat integration (#19516)
- **Description:** Added integration with [GigaChat](https://developers.sber.ru/portal/products/gigachat) embeddings. Also added support for extra fields in GigaChat LLM and fixed docs.pull/19491/head
parent
e5bdb26f76
commit
dac2e0165a
@ -0,0 +1,116 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# GigaChat\n",
|
||||
"This notebook shows how to use LangChain with [GigaChat embeddings](https://developers.sber.ru/portal/products/gigachat).\n",
|
||||
"To use you need to install ```gigachat``` python package."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install --upgrade --quiet gigachat"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"To get GigaChat credentials you need to [create account](https://developers.sber.ru/studio/login) and [get access to API](https://developers.sber.ru/docs/ru/gigachat/individuals-quickstart)\n",
|
||||
"\n",
|
||||
"## Example"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"from getpass import getpass\n",
|
||||
"\n",
|
||||
"os.environ[\"GIGACHAT_CREDENTIALS\"] = getpass()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.embeddings import GigaChatEmbeddings\n",
|
||||
"\n",
|
||||
"embeddings = GigaChatEmbeddings(verify_ssl_certs=False, scope=\"GIGACHAT_API_PERS\")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"query_result = embeddings.embed_query(\"The quick brown fox jumps over the lazy dog\")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "[0.8398333191871643,\n -0.14180311560630798,\n -0.6161925792694092,\n -0.17103666067123413,\n 1.2884578704833984]"
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"query_result[:5]"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
@ -0,0 +1,187 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from functools import cached_property
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from langchain_core.embeddings import Embeddings
|
||||
from langchain_core.pydantic_v1 import BaseModel, root_validator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MAX_BATCH_SIZE_CHARS = 1000000
|
||||
MAX_BATCH_SIZE_PARTS = 90
|
||||
|
||||
|
||||
class GigaChatEmbeddings(BaseModel, Embeddings):
|
||||
"""GigaChat Embeddings models.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
from langchain_community.embeddings.gigachat import GigaChatEmbeddings
|
||||
|
||||
embeddings = GigaChatEmbeddings(
|
||||
credentials=..., scope=..., verify_ssl_certs=False
|
||||
)
|
||||
"""
|
||||
|
||||
base_url: Optional[str] = None
|
||||
""" Base API URL """
|
||||
auth_url: Optional[str] = None
|
||||
""" Auth URL """
|
||||
credentials: Optional[str] = None
|
||||
""" Auth Token """
|
||||
scope: Optional[str] = None
|
||||
""" Permission scope for access token """
|
||||
|
||||
access_token: Optional[str] = None
|
||||
""" Access token for GigaChat """
|
||||
|
||||
model: Optional[str] = None
|
||||
"""Model name to use."""
|
||||
user: Optional[str] = None
|
||||
""" Username for authenticate """
|
||||
password: Optional[str] = None
|
||||
""" Password for authenticate """
|
||||
|
||||
timeout: Optional[float] = 600
|
||||
""" Timeout for request. By default it works for long requests. """
|
||||
verify_ssl_certs: Optional[bool] = None
|
||||
""" Check certificates for all requests """
|
||||
|
||||
ca_bundle_file: Optional[str] = None
|
||||
cert_file: Optional[str] = None
|
||||
key_file: Optional[str] = None
|
||||
key_file_password: Optional[str] = None
|
||||
# Support for connection to GigaChat through SSL certificates
|
||||
|
||||
@cached_property
|
||||
def _client(self) -> Any:
|
||||
"""Returns GigaChat API client"""
|
||||
import gigachat
|
||||
|
||||
return gigachat.GigaChat(
|
||||
base_url=self.base_url,
|
||||
auth_url=self.auth_url,
|
||||
credentials=self.credentials,
|
||||
scope=self.scope,
|
||||
access_token=self.access_token,
|
||||
model=self.model,
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
timeout=self.timeout,
|
||||
verify_ssl_certs=self.verify_ssl_certs,
|
||||
ca_bundle_file=self.ca_bundle_file,
|
||||
cert_file=self.cert_file,
|
||||
key_file=self.key_file,
|
||||
key_file_password=self.key_file_password,
|
||||
)
|
||||
|
||||
@root_validator()
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
"""Validate authenticate data in environment and python package is installed."""
|
||||
try:
|
||||
import gigachat # noqa: F401
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Could not import gigachat python package. "
|
||||
"Please install it with `pip install gigachat`."
|
||||
)
|
||||
fields = set(cls.__fields__.keys())
|
||||
diff = set(values.keys()) - fields
|
||||
if diff:
|
||||
logger.warning(f"Extra fields {diff} in GigaChat class")
|
||||
return values
|
||||
|
||||
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||
"""Embed documents using a GigaChat embeddings models.
|
||||
|
||||
Args:
|
||||
texts: The list of texts to embed.
|
||||
|
||||
Returns:
|
||||
List of embeddings, one for each text.
|
||||
"""
|
||||
result: List[List[float]] = []
|
||||
size = 0
|
||||
local_texts = []
|
||||
embed_kwargs = {}
|
||||
if self.model is not None:
|
||||
embed_kwargs["model"] = self.model
|
||||
for text in texts:
|
||||
local_texts.append(text)
|
||||
size += len(text)
|
||||
if size > MAX_BATCH_SIZE_CHARS or len(local_texts) > MAX_BATCH_SIZE_PARTS:
|
||||
for embedding in self._client.embeddings(
|
||||
texts=local_texts, **embed_kwargs
|
||||
).data:
|
||||
result.append(embedding.embedding)
|
||||
size = 0
|
||||
local_texts = []
|
||||
# Call for last iteration
|
||||
if local_texts:
|
||||
for embedding in self._client.embeddings(
|
||||
texts=local_texts, **embed_kwargs
|
||||
).data:
|
||||
result.append(embedding.embedding)
|
||||
|
||||
return result
|
||||
|
||||
async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||
"""Embed documents using a GigaChat embeddings models.
|
||||
|
||||
Args:
|
||||
texts: The list of texts to embed.
|
||||
|
||||
Returns:
|
||||
List of embeddings, one for each text.
|
||||
"""
|
||||
result: List[List[float]] = []
|
||||
size = 0
|
||||
local_texts = []
|
||||
embed_kwargs = {}
|
||||
if self.model is not None:
|
||||
embed_kwargs["model"] = self.model
|
||||
for text in texts:
|
||||
local_texts.append(text)
|
||||
size += len(text)
|
||||
if size > MAX_BATCH_SIZE_CHARS or len(local_texts) > MAX_BATCH_SIZE_PARTS:
|
||||
embeddings = await self._client.aembeddings(
|
||||
texts=local_texts, **embed_kwargs
|
||||
)
|
||||
for embedding in embeddings.data:
|
||||
result.append(embedding.embedding)
|
||||
size = 0
|
||||
local_texts = []
|
||||
# Call for last iteration
|
||||
if local_texts:
|
||||
embeddings = await self._client.aembeddings(
|
||||
texts=local_texts, **embed_kwargs
|
||||
)
|
||||
for embedding in embeddings.data:
|
||||
result.append(embedding.embedding)
|
||||
|
||||
return result
|
||||
|
||||
def embed_query(self, text: str) -> List[float]:
|
||||
"""Embed a query using a GigaChat embeddings models.
|
||||
|
||||
Args:
|
||||
text: The text to embed.
|
||||
|
||||
Returns:
|
||||
Embeddings for the text.
|
||||
"""
|
||||
return self.embed_documents(texts=[text])[0]
|
||||
|
||||
async def aembed_query(self, text: str) -> List[float]:
|
||||
"""Embed a query using a GigaChat embeddings models.
|
||||
|
||||
Args:
|
||||
text: The text to embed.
|
||||
|
||||
Returns:
|
||||
Embeddings for the text.
|
||||
"""
|
||||
docs = await self.aembed_documents(texts=[text])
|
||||
return docs[0]
|
Loading…
Reference in New Issue