mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-11-26 09:57:24 +03:00
gpt 3.5 / customize models (site: ora.sh)
This commit is contained in:
parent
66cd4b0664
commit
493c37a81e
36
ora/__init__.py
Normal file
36
ora/__init__.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
from ora.model import CompletionModel
|
||||||
|
from ora.typing import OraResponse
|
||||||
|
from requests import post
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
class Completion:
|
||||||
|
def create(
|
||||||
|
model : CompletionModel,
|
||||||
|
prompt: str,
|
||||||
|
conversationId: str or None = None) -> OraResponse:
|
||||||
|
|
||||||
|
extra = {
|
||||||
|
'conversationId': conversationId} if conversationId else {}
|
||||||
|
|
||||||
|
response = post('https://ora.sh/api/conversation', json = extra | {
|
||||||
|
'chatbotId': model.id,
|
||||||
|
'input' : prompt,
|
||||||
|
'userId' : model.createdBy}).json()
|
||||||
|
|
||||||
|
return OraResponse({
|
||||||
|
'id' : response['conversationId'],
|
||||||
|
'object' : 'text_completion',
|
||||||
|
'created': int(time()),
|
||||||
|
'model' : model.slug,
|
||||||
|
'choices': [{
|
||||||
|
'text' : response['response'],
|
||||||
|
'index' : 0,
|
||||||
|
'logprobs' : None,
|
||||||
|
'finish_reason' : 'stop'
|
||||||
|
}],
|
||||||
|
'usage': {
|
||||||
|
'prompt_tokens' : len(prompt),
|
||||||
|
'completion_tokens' : len(response['response']),
|
||||||
|
'total_tokens' : len(prompt) + len(response['response'])
|
||||||
|
}
|
||||||
|
})
|
32
ora/model.py
Normal file
32
ora/model.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from uuid import uuid4
|
||||||
|
from requests import post
|
||||||
|
|
||||||
|
class CompletionModel:
|
||||||
|
system_prompt = None
|
||||||
|
description = None
|
||||||
|
createdBy = None
|
||||||
|
createdAt = None
|
||||||
|
slug = None
|
||||||
|
id = None
|
||||||
|
|
||||||
|
def create(
|
||||||
|
system_prompt: str = 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible',
|
||||||
|
description : str = 'ChatGPT Openai Language Model',
|
||||||
|
name : str = 'gpt-3.5'):
|
||||||
|
|
||||||
|
CompletionModel.system_prompt = system_prompt
|
||||||
|
CompletionModel.description = description
|
||||||
|
CompletionModel.slug = name
|
||||||
|
|
||||||
|
response = post('https://ora.sh/api/assistant', json = {
|
||||||
|
'prompt' : system_prompt,
|
||||||
|
'userId' : f'auto:{uuid4()}',
|
||||||
|
'name' : name,
|
||||||
|
'description': description})
|
||||||
|
|
||||||
|
CompletionModel.id = response.json()['id']
|
||||||
|
CompletionModel.createdBy = response.json()['createdBy']
|
||||||
|
CompletionModel.createdAt = response.json()['createdAt']
|
||||||
|
|
||||||
|
return CompletionModel
|
||||||
|
|
39
ora/typing.py
Normal file
39
ora/typing.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
class OraResponse:
|
||||||
|
|
||||||
|
class Completion:
|
||||||
|
|
||||||
|
class Choices:
|
||||||
|
def __init__(self, choice: dict) -> None:
|
||||||
|
self.text = choice['text']
|
||||||
|
self.content = self.text.encode()
|
||||||
|
self.index = choice['index']
|
||||||
|
self.logprobs = choice['logprobs']
|
||||||
|
self.finish_reason = choice['finish_reason']
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f'''<__main__.APIResponse.Completion.Choices(\n text = {self.text.encode()},\n index = {self.index},\n logprobs = {self.logprobs},\n finish_reason = {self.finish_reason})object at 0x1337>'''
|
||||||
|
|
||||||
|
def __init__(self, choices: dict) -> None:
|
||||||
|
self.choices = [self.Choices(choice) for choice in choices]
|
||||||
|
|
||||||
|
class Usage:
|
||||||
|
def __init__(self, usage_dict: dict) -> None:
|
||||||
|
self.prompt_tokens = usage_dict['prompt_tokens']
|
||||||
|
self.completion_tokens = usage_dict['completion_tokens']
|
||||||
|
self.total_tokens = usage_dict['total_tokens']
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'''<__main__.APIResponse.Usage(\n prompt_tokens = {self.prompt_tokens},\n completion_tokens = {self.completion_tokens},\n total_tokens = {self.total_tokens})object at 0x1337>'''
|
||||||
|
|
||||||
|
def __init__(self, response_dict: dict) -> None:
|
||||||
|
|
||||||
|
self.response_dict = response_dict
|
||||||
|
self.id = response_dict['id']
|
||||||
|
self.object = response_dict['object']
|
||||||
|
self.created = response_dict['created']
|
||||||
|
self.model = response_dict['model']
|
||||||
|
self.completion = self.Completion(response_dict['choices'])
|
||||||
|
self.usage = self.Usage(response_dict['usage'])
|
||||||
|
|
||||||
|
def json(self) -> dict:
|
||||||
|
return self.response_dict
|
Loading…
Reference in New Issue
Block a user