summaryrefslogtreecommitdiffstats
path: root/ora/__init__.py
blob: 546e09409f91d064e072c60ef79c29f39255388f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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'])
            }
        })