Skip to content

Commit

Permalink
community[minor]: Added GigaChat Embeddings support + updated previou…
Browse files Browse the repository at this point in the history
…s GigaChat integration (langchain-ai#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.
  • Loading branch information
Mikelarg authored and chrispy-snps committed Mar 30, 2024
1 parent 6f0dfd8 commit 1cd039e
Show file tree
Hide file tree
Showing 9 changed files with 547 additions and 61 deletions.
24 changes: 14 additions & 10 deletions docs/docs/integrations/chat/gigachat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": true,
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"source": [
Expand All @@ -28,13 +31,14 @@
"collapsed": false
},
"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/api/integration)\n",
"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"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand All @@ -48,20 +52,20 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from langchain_community.chat_models import GigaChat\n",
"\n",
"chat = GigaChat(verify_ssl_certs=False)"
"chat = GigaChat(verify_ssl_certs=False, scope=\"GIGACHAT_API_PERS\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 8,
"metadata": {
"collapsed": false
},
Expand All @@ -70,7 +74,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"What do you get when you cross a goat and a skunk? A smelly goat!\n"
"The capital of Russia is Moscow.\n"
]
}
],
Expand All @@ -81,10 +85,10 @@
" SystemMessage(\n",
" content=\"You are a helpful AI that shares everything you know. Talk in English.\"\n",
" ),\n",
" HumanMessage(content=\"Tell me a joke\"),\n",
" HumanMessage(content=\"What is capital of Russia?\"),\n",
"]\n",
"\n",
"print(chat(messages).content)"
"print(chat.invoke(messages).content)"
]
}
],
Expand Down
20 changes: 12 additions & 8 deletions docs/docs/integrations/llms/gigachat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": true,
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"source": [
Expand All @@ -28,13 +31,14 @@
"collapsed": false
},
"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/api/integration)\n",
"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"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand All @@ -48,20 +52,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from langchain_community.llms import GigaChat\n",
"\n",
"llm = GigaChat(verify_ssl_certs=False)"
"llm = GigaChat(verify_ssl_certs=False, scope=\"GIGACHAT_API_PERS\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"metadata": {
"collapsed": false
},
Expand All @@ -84,8 +88,8 @@
"\n",
"llm_chain = LLMChain(prompt=prompt, llm=llm)\n",
"\n",
"generated = llm_chain.run(country=\"Russia\")\n",
"print(generated)"
"generated = llm_chain.invoke(input={\"country\": \"Russia\"})\n",
"print(generated[\"text\"])"
]
}
],
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/integrations/providers/salute_devices.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ See a [usage example](/docs/integrations/chat/gigachat).

```python
from langchain_community.chat_models import GigaChat
```

## Embeddings

See a [usage example](/docs/integrations/text_embedding/gigachat).

```python
from langchain_community.embeddings import GigaChatEmbeddings
```
116 changes: 116 additions & 0 deletions docs/docs/integrations/text_embedding/gigachat.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 1cd039e

Please sign in to comment.