File size: 2,474 Bytes
fe0ce42
 
 
 
9fa6897
fe0ce42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fa6897
 
 
fe0ce42
9fa6897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0ce42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fa6897
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import json
import requests
import tiktoken
import logging
from difflib import Differ


def json_validator(
    text: str,
    openai_key: str,
    retry: int = 3,
    model: str = "text-davinci-003"
):
    
    encoder = tiktoken.encoding_for_model(model)
    
    for _ in range(retry):
        try:
            return json.loads(text)
        except Exception:
            
            
            prompt = f"Modify the following into a valid json format:\n{text}"
            prompt_token_length = len(encoder.encode(prompt))

            data = {
                "model": model,
                "prompt": prompt,
                "max_tokens": 4097 - prompt_token_length - 64
            }
            headers = {
                "Content-Type": "application/json",
                "Authorization": f"Bearer {openai_key}"
            }
            for _ in range(retry):
                response = requests.post(
                    'https://api.openai.com/v1/completions',
                    json=data,
                    headers=headers,
                    timeout=300
                )
                if response.status_code != 200:
                    logging.warning(f'fetch openai chat retry: {response.text}')
                    continue
                text = response.json()['choices'][0]['text']
                break
            
            if response.status_code != 200:
                raise Exception(response.json()['error'])
            
    return text


def fetch_chat(
    prompt: str,
    openai_key: str,
    retry: int = 3,
    model: str = "gpt-3.5-turbo-16k"
):
    data = {
        "model": model,
        "messages": [{"role": "user", "content": prompt}]
    }
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {openai_key}"
    }
    for _ in range(retry):
        response = requests.post(
            'https://api.openai.com/v1/chat/completions',
            json=data,
            headers=headers,
            timeout=300
        )
        if response.status_code != 200:
            logging.warning(f'fetch openai chat retry: {response.text}')
            continue
        result = response.json()['choices'][0]['message']['content']
        return result
    
    return response.json()["error"]


def diff_texts(text1: str, text2: str) -> list:
    d = Differ()
    return [
        (token[2:], token[0] if token[0] != " " else None)
        for token in d.compare(text1, text2)
    ]