Skip to content

Commit

Permalink
feat: add yandexgpt api wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeryVerkhoturov committed Aug 9, 2023
1 parent 26bbfdf commit 3be2cdc
Show file tree
Hide file tree
Showing 7 changed files with 2,331 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ logging
/public/service-worker.js
/public/service-worker.js.map
/public/workbox-*.js
/public/workbox-*.js.map
/public/workbox-*.js.map

# Course work
course-work
1 change: 1 addition & 0 deletions course-work/init/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY=
2 changes: 2 additions & 0 deletions course-work/init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.env
2,083 changes: 2,083 additions & 0 deletions course-work/init/poetry.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions course-work/init/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.poetry]
name = "init"
version = "0.1.0"
description = ""
authors = ["Valery Verkhoturov <verkhoturov.valery@icloud.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"
jupyter = "^1.0.0"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
101 changes: 101 additions & 0 deletions course-work/init/yagpt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import os
import requests
from typing import List

KEY = os.getenv("KEY")
if (KEY is None):
raise Exception("KEY does not exist in .env")


class Message:
def __init__(self, role: str, text: str):
self.role = role
self.text = text


class TextGeneration:
@staticmethod
def chat(messages: List[Message], instruction: str, temperature: float = 0.5, max_tokens: int = 2000):
url = "https://llm.api.cloud.yandex.net/llm/v1alpha/chat"

headers = {
"Content-Type": "application/json",
"Authorization": f"Api-Key {KEY}"
}

data = {
"model": "general",
"generationOptions": {
# "partialResults": True,
"temperature": temperature,
"maxTokens": max_tokens
},
"messages": list(map(lambda msg: vars(msg), messages)),
"instructionText": instruction
}

response = requests.post(url, json=data, headers=headers)

if response.status_code == 200:
response_data = response.json()
# Example:
# {'result': {'message': {'role': 'Ассистент', 'text': 'Привет! Чем я могу вам помочь?'}, 'num_tokens': '21'}}
return response_data
else:
print("Request failed with status code:", response.status_code)

@staticmethod
def instruct(request_text: str, instruction: str, temperature: float = 0.5, max_tokens: int = 2000):
url = "https://llm.api.cloud.yandex.net/llm/v1alpha/instruct"

headers = {
"Content-Type": "application/json",
"Authorization": f"Api-Key {KEY}"
}

data = {
"model": "general",
"generationOptions": {
# "partialResults": True,
"temperature": temperature,
"maxTokens": max_tokens
},
"requestText": request_text,
"instructionText": instruction
}

response = requests.post(url, json=data, headers=headers)

if response.status_code == 200:
response_data = response.json()
# Example:
# {'result': {'alternatives': [{'text': 'Здравствуйте! Я всегда готов помочь вам с любыми вопросами или задачами. Чем я могу вам помочь сегодня?', 'score': -0.491208016872406, 'num_tokens': '20'}], 'num_prompt_tokens': '8'}}
return response_data
else:
print("Request failed with status code:", response.status_code)


class Tokenizer:
@staticmethod
def tokenize(text: str):
url = "https://llm.api.cloud.yandex.net/llm/v1alpha/tokenize"

headers = {
"Content-Type": "application/json",
"Authorization": f"Api-Key {KEY}"
}

data = {
"model": "general",
"text": text
}

response = requests.post(url, json=data, headers=headers)

if response.status_code == 200:
response_data = response.json()
# Example:
# {'tokens': [{'id': '0', 'text': '<s>', 'special': True}, {'id': '763', 'text': '▁как', 'special': False}, {'id': '1731', 'text': '▁дела', 'special': False}, {'id': '125917', 'text': '?', 'special': False}, {'id': '4', 'text': '[SEP]', 'special': True}]}
return response_data
else:
print("Request failed with status code:", response.status_code)
125 changes: 125 additions & 0 deletions course-work/init/yandex-gpt-generation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2023-08-09T19:43:16.657743Z",
"start_time": "2023-08-09T19:43:15.993866Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: python-dotenv in /Users/valerijverhoturov/Library/Caches/pypoetry/virtualenvs/init-sxKJvSVg-py3.10/lib/python3.10/site-packages (1.0.0)\r\n",
"\r\n",
"\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip is available: \u001B[0m\u001B[31;49m23.1.2\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m23.2.1\u001B[0m\r\n",
"\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip install --upgrade pip\u001B[0m\r\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install python-dotenv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"%load_ext dotenv\n",
"%dotenv"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-08-09T19:43:17.534751Z",
"start_time": "2023-08-09T19:43:17.522902Z"
}
},
"id": "802316cf6a0e8b4a"
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'result': {'message': {'role': 'Ассистент', 'text': 'Хорошо, спасибо. А у вас?'}, 'num_tokens': '23'}}\n",
"{'result': {'alternatives': [{'text': 'Я - искусственный интеллект, созданный для помощи людям. Моя задача - отвечать на вопросы и предоставлять информацию по запросу. Как я могу вам помочь?', 'score': -0.4338773488998413, 'num_tokens': '28'}], 'num_prompt_tokens': '8'}}\n",
"{'tokens': [{'id': '0', 'text': '<s>', 'special': True}, {'id': '763', 'text': '▁как', 'special': False}, {'id': '1731', 'text': '▁дела', 'special': False}, {'id': '125917', 'text': '?', 'special': False}, {'id': '4', 'text': '[SEP]', 'special': True}]}\n"
]
}
],
"source": [
"from yagpt import Tokenizer, TextGeneration, Message\n",
"\n",
"# Test \n",
"print(TextGeneration.chat([Message(\"user\", \"как дела?\")], \"ты помощник\", 1))\n",
"print(TextGeneration.instruct(\"как дела?\", \"ты помощник\", 1)) \n",
"print(Tokenizer.tokenize(\"как дела?\"))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-08-09T19:45:00.713876Z",
"start_time": "2023-08-09T19:44:54.427734Z"
}
},
"id": "4fb1f7e08670c1a6"
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-08-09T19:20:55.043596Z",
"start_time": "2023-08-09T19:20:55.023774Z"
}
},
"id": "db86c5f8a3da104b"
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"id": "a788287b5dd8be4c"
}
],
"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": 5
}

0 comments on commit 3be2cdc

Please sign in to comment.