async sqlite
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import aiohttp
|
||||
import base64
|
||||
from openai import AsyncOpenAI
|
||||
from database import db
|
||||
from database import Database
|
||||
|
||||
API_KEY = "eh"
|
||||
MODEL = "p620"
|
||||
@@ -13,12 +13,13 @@ NAME_PROMPT = "reply with your name, nothing else, no punctuation"
|
||||
|
||||
|
||||
class Conversation:
|
||||
def __init__(self, client, name, prompt, convo_id):
|
||||
def __init__(self, client, name, prompt, convo_id, db):
|
||||
self.history = [{"role": "system", "content": prompt}]
|
||||
self.bot_name = name
|
||||
self.last_messages = []
|
||||
self.client = client
|
||||
self.id = convo_id
|
||||
self.db = db
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
@@ -26,29 +27,32 @@ class Conversation:
|
||||
f"{self.history}"
|
||||
)
|
||||
|
||||
def save(self):
|
||||
db.save(self.id, self.history, self.bot_name, self.last_messages)
|
||||
async def save(self):
|
||||
await self.db.save(
|
||||
self.id, self.history, self.bot_name, self.last_messages
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get(cls, key, base_url):
|
||||
convo_data = db.get(key)
|
||||
async def get(cls, key, base_url, db):
|
||||
convo_data = await db.get_conversation(key)
|
||||
if convo_data:
|
||||
history, bot_name, last_messages = convo_data
|
||||
client = AsyncOpenAI(base_url=base_url, api_key=API_KEY)
|
||||
convo = cls(client, bot_name, history[0]['content'], key)
|
||||
convo = cls(client, bot_name, history[0]['content'], key, db)
|
||||
convo.history = history
|
||||
convo.last_messages = last_messages
|
||||
return convo
|
||||
return await Conversation.create(key, base_url)
|
||||
return await Conversation.create(key, base_url, db)
|
||||
|
||||
@classmethod
|
||||
async def create(cls, key, base_url, prompt=None):
|
||||
async def create(cls, key, base_url, db, prompt=None):
|
||||
client = AsyncOpenAI(base_url=base_url, api_key=API_KEY)
|
||||
if not prompt:
|
||||
convo = cls(client, DEFAULT_NAME, DEFAULT_SYSTEM_PROMPT, key)
|
||||
convo = cls(client, DEFAULT_NAME, DEFAULT_SYSTEM_PROMPT, key, db)
|
||||
else:
|
||||
convo = cls(client, await cls.get_name(client, prompt), prompt, key)
|
||||
convo.save()
|
||||
name = await cls.get_name(client, prompt)
|
||||
convo = cls(client, name, prompt, key, db)
|
||||
await convo.save()
|
||||
return convo
|
||||
|
||||
@classmethod
|
||||
@@ -68,10 +72,10 @@ class Conversation:
|
||||
{"role": "assistant", "content": assistant},
|
||||
])
|
||||
|
||||
def pop(self):
|
||||
async def pop(self):
|
||||
if len(self.history) >= 3:
|
||||
self.history = self.history[:-2]
|
||||
self.save()
|
||||
await self.save()
|
||||
|
||||
async def generate(self, text, media=tuple()):
|
||||
# prepare text part
|
||||
@@ -115,4 +119,3 @@ class Conversation:
|
||||
response = llm_response.choices[0].message.content
|
||||
self.history[-1] = {"role": "assistant", "content": response}
|
||||
return response
|
||||
|
||||
|
Reference in New Issue
Block a user