-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from ai-forever/dev
Merge
- Loading branch information
Showing
78 changed files
with
3,532 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import base64 | ||
|
||
from gigachat import GigaChat | ||
from gigachat.models import Image | ||
|
||
|
||
def get_image(file_path: str = './giga_img.jpg'): | ||
with GigaChat(credentials=..., | ||
verify_ssl_certs=False) as giga: | ||
response: Image = giga.get_image(file_id=...) | ||
|
||
# Сохранить изображение в файл | ||
with open(file_path, mode="wb") as fd: | ||
fd.write(base64.b64decode(response.content)) | ||
|
||
|
||
async def get_image_async(file_path: str = './giga_img.jpg'): | ||
from aiofile import async_open | ||
|
||
async with GigaChat(credentials=..., | ||
verify_ssl_certs=False) as giga: | ||
response: Image = await giga.aget_image(file_id=...) | ||
|
||
# Сохранить изображение в файл | ||
async with async_open(file_path, 'wb') as afp: | ||
await afp.write(base64.b64decode(response.content)) |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "gigachat" | ||
version = "0.1.22" | ||
version = "0.1.29" | ||
description = "GigaChat. Python-library for GigaChain and LangChain" | ||
authors = ["Konstantin Krestnikov <[email protected]>", "Sergey Malyshev <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from typing import IO, Mapping, Optional, Tuple, Union | ||
|
||
FileContent = Union[IO[bytes], bytes, str] | ||
FileTypes = Union[ | ||
# file (or bytes) | ||
FileContent, | ||
# (filename, file (or bytes)) | ||
Tuple[Optional[str], FileContent], | ||
# (filename, file (or bytes), content_type) | ||
Tuple[Optional[str], FileContent, Optional[str]], | ||
# (filename, file (or bytes), content_type, headers) | ||
Tuple[Optional[str], FileContent, Optional[str], Mapping[str, str]], | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from http import HTTPStatus | ||
from typing import Any, Dict, Optional | ||
|
||
import httpx | ||
|
||
from gigachat.api.utils import build_headers | ||
from gigachat.exceptions import AuthenticationError, ResponseError | ||
from gigachat.models.assistants import Assistants | ||
|
||
|
||
def _get_kwargs( | ||
*, | ||
assistant_id: Optional[str] = None, | ||
access_token: Optional[str] = None, | ||
) -> Dict[str, Any]: | ||
headers = build_headers(access_token) | ||
params = { | ||
"method": "GET", | ||
"url": "/assistants", | ||
"headers": headers, | ||
} | ||
if assistant_id: | ||
params["params"] = {"assistant_id": assistant_id} | ||
return params | ||
|
||
|
||
def _build_response(response: httpx.Response) -> Assistants: | ||
if response.status_code == HTTPStatus.OK: | ||
return Assistants(**response.json()) | ||
elif response.status_code == HTTPStatus.UNAUTHORIZED: | ||
raise AuthenticationError(response.url, response.status_code, response.content, response.headers) | ||
else: | ||
raise ResponseError(response.url, response.status_code, response.content, response.headers) | ||
|
||
|
||
def sync( | ||
client: httpx.Client, | ||
*, | ||
assistant_id: Optional[str] = None, | ||
access_token: Optional[str] = None, | ||
) -> Assistants: | ||
"""Возвращает массив объектов с данными доступных ассистентов""" | ||
kwargs = _get_kwargs(assistant_id=assistant_id, access_token=access_token) | ||
response = client.request(**kwargs) | ||
return _build_response(response) | ||
|
||
|
||
async def asyncio( | ||
client: httpx.AsyncClient, | ||
*, | ||
assistant_id: Optional[str] = None, | ||
access_token: Optional[str] = None, | ||
) -> Assistants: | ||
"""Возвращает массив объектов с данными доступных ассистентов""" | ||
kwargs = _get_kwargs(assistant_id=assistant_id, access_token=access_token) | ||
response = await client.request(**kwargs) | ||
return _build_response(response) |
60 changes: 60 additions & 0 deletions
60
src/gigachat/api/assistants/post_assistant_files_delete.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from http import HTTPStatus | ||
from typing import Any, Dict, Optional | ||
|
||
import httpx | ||
|
||
from gigachat.api.utils import build_headers | ||
from gigachat.exceptions import AuthenticationError, ResponseError | ||
from gigachat.models.assistants import AssistantFileDelete | ||
|
||
|
||
def _get_kwargs( | ||
*, | ||
assistant_id: str, | ||
file_id: str, | ||
access_token: Optional[str] = None, | ||
) -> Dict[str, Any]: | ||
headers = build_headers(access_token) | ||
|
||
return { | ||
"method": "POST", | ||
"url": "/assistants/files/delete", | ||
"json": { | ||
"assistant_id": assistant_id, | ||
"file_id": file_id, | ||
}, | ||
"headers": headers, | ||
} | ||
|
||
|
||
def _build_response(response: httpx.Response) -> AssistantFileDelete: | ||
if response.status_code == HTTPStatus.OK: | ||
return AssistantFileDelete(**response.json()) | ||
elif response.status_code == HTTPStatus.UNAUTHORIZED: | ||
raise AuthenticationError(response.url, response.status_code, response.content, response.headers) | ||
else: | ||
raise ResponseError(response.url, response.status_code, response.content, response.headers) | ||
|
||
|
||
def sync( | ||
client: httpx.Client, | ||
*, | ||
assistant_id: str, | ||
file_id: str, | ||
access_token: Optional[str] = None, | ||
) -> AssistantFileDelete: | ||
kwargs = _get_kwargs(assistant_id=assistant_id, file_id=file_id, access_token=access_token) | ||
response = client.request(**kwargs) | ||
return _build_response(response) | ||
|
||
|
||
async def asyncio( | ||
client: httpx.AsyncClient, | ||
*, | ||
assistant_id: str, | ||
file_id: str, | ||
access_token: Optional[str] = None, | ||
) -> AssistantFileDelete: | ||
kwargs = _get_kwargs(assistant_id=assistant_id, file_id=file_id, access_token=access_token) | ||
response = await client.request(**kwargs) | ||
return _build_response(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from http import HTTPStatus | ||
from typing import Any, Dict, List, Optional | ||
|
||
import httpx | ||
|
||
from gigachat.api.utils import build_headers | ||
from gigachat.exceptions import AuthenticationError, ResponseError | ||
from gigachat.models.assistants import Assistant | ||
|
||
|
||
def _get_kwargs( | ||
*, | ||
assistant_id: str, | ||
name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
instructions: Optional[str] = None, | ||
file_ids: Optional[List[str]] = None, | ||
metadata: Optional[Dict[str, Any]] = None, | ||
access_token: Optional[str] = None, | ||
) -> Dict[str, Any]: | ||
headers = build_headers(access_token) | ||
|
||
return { | ||
"method": "POST", | ||
"url": "/assistants/modify", | ||
"json": { | ||
"assistant_id": assistant_id, | ||
"name": name, | ||
"description": description, | ||
"instructions": instructions, | ||
"file_ids": file_ids, | ||
"metadata": metadata, | ||
}, | ||
"headers": headers, | ||
} | ||
|
||
|
||
def _build_response(response: httpx.Response) -> Assistant: | ||
if response.status_code == HTTPStatus.OK: | ||
return Assistant(**response.json()) | ||
elif response.status_code == HTTPStatus.UNAUTHORIZED: | ||
raise AuthenticationError(response.url, response.status_code, response.content, response.headers) | ||
else: | ||
raise ResponseError(response.url, response.status_code, response.content, response.headers) | ||
|
||
|
||
def sync( | ||
client: httpx.Client, | ||
*, | ||
assistant_id: str, | ||
name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
instructions: Optional[str] = None, | ||
file_ids: Optional[List[str]] = None, | ||
metadata: Optional[Dict[str, Any]] = None, | ||
access_token: Optional[str] = None, | ||
) -> Assistant: | ||
kwargs = _get_kwargs( | ||
assistant_id=assistant_id, | ||
name=name, | ||
description=description, | ||
instructions=instructions, | ||
file_ids=file_ids, | ||
metadata=metadata, | ||
access_token=access_token, | ||
) | ||
response = client.request(**kwargs) | ||
return _build_response(response) | ||
|
||
|
||
async def asyncio( | ||
client: httpx.AsyncClient, | ||
*, | ||
assistant_id: str, | ||
name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
instructions: Optional[str] = None, | ||
file_ids: Optional[List[str]] = None, | ||
metadata: Optional[Dict[str, Any]] = None, | ||
access_token: Optional[str] = None, | ||
) -> Assistant: | ||
kwargs = _get_kwargs( | ||
assistant_id=assistant_id, | ||
name=name, | ||
description=description, | ||
instructions=instructions, | ||
file_ids=file_ids, | ||
metadata=metadata, | ||
access_token=access_token, | ||
) | ||
response = await client.request(**kwargs) | ||
return _build_response(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from http import HTTPStatus | ||
from typing import Any, Dict, List, Optional | ||
|
||
import httpx | ||
|
||
from gigachat.api.utils import build_headers | ||
from gigachat.exceptions import AuthenticationError, ResponseError | ||
from gigachat.models.assistants import CreateAssistant | ||
|
||
|
||
def _get_kwargs( | ||
*, | ||
model: str, | ||
name: str, | ||
description: Optional[str] = None, | ||
instructions: str, | ||
file_ids: Optional[List[str]] = None, | ||
metadata: Optional[Dict[str, Any]] = None, | ||
access_token: Optional[str] = None, | ||
) -> Dict[str, Any]: | ||
headers = build_headers(access_token) | ||
|
||
return { | ||
"method": "POST", | ||
"url": "/assistants", | ||
"json": { | ||
"model": model, | ||
"name": name, | ||
"description": description, | ||
"instructions": instructions, | ||
"file_ids": file_ids, | ||
"metadata": metadata, | ||
}, | ||
"headers": headers, | ||
} | ||
|
||
|
||
def _build_response(response: httpx.Response) -> CreateAssistant: | ||
if response.status_code == HTTPStatus.OK: | ||
return CreateAssistant(**response.json()) | ||
elif response.status_code == HTTPStatus.UNAUTHORIZED: | ||
raise AuthenticationError(response.url, response.status_code, response.content, response.headers) | ||
else: | ||
raise ResponseError(response.url, response.status_code, response.content, response.headers) | ||
|
||
|
||
def sync( | ||
client: httpx.Client, | ||
*, | ||
model: str, | ||
name: str, | ||
description: Optional[str] = None, | ||
instructions: str, | ||
file_ids: Optional[List[str]] = None, | ||
metadata: Optional[Dict[str, Any]] = None, | ||
access_token: Optional[str] = None, | ||
) -> CreateAssistant: | ||
"""Создание ассистента""" | ||
kwargs = _get_kwargs( | ||
model=model, | ||
name=name, | ||
description=description, | ||
instructions=instructions, | ||
file_ids=file_ids, | ||
metadata=metadata, | ||
access_token=access_token, | ||
) | ||
response = client.request(**kwargs) | ||
return _build_response(response) | ||
|
||
|
||
async def asyncio( | ||
client: httpx.AsyncClient, | ||
*, | ||
model: str, | ||
name: str, | ||
description: Optional[str] = None, | ||
instructions: str, | ||
file_ids: Optional[List[str]] = None, | ||
metadata: Optional[Dict[str, Any]] = None, | ||
access_token: Optional[str] = None, | ||
) -> CreateAssistant: | ||
"""Создание ассистента""" | ||
kwargs = _get_kwargs( | ||
model=model, | ||
name=name, | ||
description=description, | ||
instructions=instructions, | ||
file_ids=file_ids, | ||
metadata=metadata, | ||
access_token=access_token, | ||
) | ||
response = await client.request(**kwargs) | ||
return _build_response(response) |
Oops, something went wrong.