-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletion.py
81 lines (66 loc) · 1.92 KB
/
completion.py
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
from enum import Enum
from dataclasses import dataclass
import openai
from openai import OpenAI
client = OpenAI()
from moderation import moderate_message
from typing import Optional, List
from constants import (
BOT_INSTRUCTIONS,
BOT_NAME,
EXAMPLE_CONVOS,
)
import discord
from base import Message, Prompt, Conversation
from utils import split_into_shorter_messages, close_thread, logger
import tiktoken
import os
from dotenv import load_dotenv
import MySQLdb
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import time
from requests.exceptions import Timeout
import asyncio
load_dotenv()
encoding = tiktoken.encoding_for_model("gpt-4")
def simple_token_counter(text):
token_count = 0
for word in text.split():
# Very simplified: count every character or punctuation as a separate token
token_count += len(word)
return token_count
def limit_tokens(strings, max_tokens):
total_tokens = 0
limited_strings = []
for string in strings:
tokens_in_string = simple_token_counter(string)
if total_tokens + tokens_in_string > max_tokens:
break
total_tokens += tokens_in_string
limited_strings.append(string)
return limited_strings
def limit_string_tokens(string, max_tokens):
if len(string) > max_tokens:
string = string[:max_tokens]
return string
def num_tokens_from_string(string: str) -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.encoding_for_model("gpt-4")
num_tokens = len(encoding.encode(string))
return num_tokens
MY_BOT_NAME = BOT_NAME
MY_BOT_EXAMPLE_CONVOS = EXAMPLE_CONVOS
class CompletionResult(Enum):
OK = 0
TOO_LONG = 1
INVALID_REQUEST = 2
OTHER_ERROR = 3
MODERATION_FLAGGED = 4
MODERATION_BLOCKED = 5
@dataclass
class CompletionData:
status: CompletionResult
reply_text: Optional[str]
status_text: Optional[str]