Skip to content

Commit

Permalink
docs: add sections, subsections generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeryVerkhoturov committed Aug 13, 2023
1 parent 0aa970b commit cdc3cbf
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 75 deletions.
3 changes: 2 additions & 1 deletion course-work/init/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
KEY=
YANDEX_GPT_KEY=
ELSEVIER_KEY=
37 changes: 37 additions & 0 deletions course-work/init/elsevier/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import requests

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

class Elsevier:
@staticmethod
def search(query: str, show: int = 25, offset: int = 0, order_by: str = 'relevance', open_access: bool = True):
url = "https://api.elsevier.com/content/search/sciencedirect"

headers = {
"Content-Type": "application/json",
"x-els-apikey": ELSEVIER_KEY
}

data = {
"qs": query,
"filters": {
"openAccess": open_access
},
"display": {
"offset": offset,
"show": show,
"sortBy": order_by
}
}

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

if response.status_code == 200:
response_data = response.json()

return response_data
else:
print("Request failed with status code:", response.status_code)
42 changes: 33 additions & 9 deletions course-work/init/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions course-work/init/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
jupyter = "^1.0.0"
python-dotenv = "^1.0.0"
toml = "^0.10.2"


[build-system]
Expand Down
60 changes: 34 additions & 26 deletions course-work/init/yagpt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import requests
from typing import List

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


class Message:
Expand All @@ -15,12 +15,16 @@ def __init__(self, role: str, text: str):

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

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

data = {
Expand All @@ -33,24 +37,28 @@ def chat(messages: List[Message], instruction: str, temperature: float = 0.5, ma
"messages": list(map(lambda msg: vars(msg), messages)),
"instructionText": instruction
}
for retry in range(retries):
response = requests.post(url, json=data, headers=headers)

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

if response.status_code == 200:
response_data = response.json()
# Example:
# {'result': {'message': {'role': 'Ассистент', 'text': 'Text'}, 'num_tokens': '21'}}
return response_data
else:
print("Request failed with status code:", response.status_code)
if response.status_code == 200:
response_data = response.json()
# Example:
# {'result': {'message': {'role': 'Ассистент', 'text': '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):
def instruct(request_text: str,
instruction: str,
temperature: float = 0.5,
max_tokens: int = 2000,
retries: int = 1):
url = "https://llm.api.cloud.yandex.net/llm/v1alpha/instruct"

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

data = {
Expand All @@ -63,16 +71,16 @@ def instruct(request_text: str, instruction: str, temperature: float = 0.5, max_
"requestText": request_text,
"instructionText": instruction
}
for retry in range(retries):
response = requests.post(url, json=data, headers=headers)

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

if response.status_code == 200:
response_data = response.json()
# Example:
# {'result': {'alternatives': [{'text': 'Text', 'score': -0.491208016872406, 'num_tokens': '20'}], 'num_prompt_tokens': '8'}}
return response_data
else:
print("Request failed with status code:", response.status_code)
if response.status_code == 200:
response_data = response.json()
# Example:
# {'result': {'alternatives': [{'text': 'Text', 'score': -0.491208016872406, 'num_tokens': '20'}], 'num_prompt_tokens': '8'}}
return response_data
else:
print(f"Request failed with status code: {response.status_code}, retry number: {retry + 1}")


class Tokenizer:
Expand All @@ -82,7 +90,7 @@ def tokenize(text: str):

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

data = {
Expand Down
Loading

0 comments on commit cdc3cbf

Please sign in to comment.