feat: 🎸 add trials for API usage

This commit is contained in:
Grey_D
2023-04-29 22:29:39 +08:00
parent 27dfd7706d
commit 46034f9a9e
3 changed files with 31 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ import re
import time
from typing import Any, Dict, List, Tuple
from uuid import uuid1
import openai
import loguru
import requests
@@ -56,6 +57,29 @@ class Conversation:
return self.conversation_id == other.conversation_id
def chatgpt_completion(history: List) -> str:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=history,
)
return response["choices"][0]["message"]["content"]
class ChatGPTAPI:
def __init__(self, config: ChatGPTConfig):
self.config = config
openai.api_key = chatgpt_config.openai_key
def send_message(self, message):
history = [{"role": "user", "content": message}]
response = chatgpt_completion(history)
return response
def extract_code_fragments(self, text):
code_fragments = re.findall(r"```(.*?)```", text, re.DOTALL)
return code_fragments
class ChatGPT:
def __init__(self, config: ChatGPTConfig):
self.config = config