CoolFace
Apppublic

philipk22/fastapi-rag-react

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
openai_utils.py34 linesDownload Raw Back to backend
1from typing import List, Dict2from openai import AsyncOpenAI3import os4 5class SystemRolePrompt:6    def __init__(self, template: str):7        self.template = template8 9    def create_message(self) -> Dict[str, str]:10        return {11            "role": "system",12            "content": self.template13        }14 15class UserRolePrompt:16    def __init__(self, template: str):17        self.template = template18 19    def create_message(self, **kwargs) -> Dict[str, str]:20        return {21            "role": "user",22            "content": self.template.format(**kwargs)23        }24 25class ChatOpenAI:26    def __init__(self):27        self.client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))28 29    async def acomplete(self, messages: List[Dict[str, str]]) -> str:30        response = await self.client.chat.completions.create(31            model="gpt-3.5-turbo",32            messages=messages33        )34        return response.choices[0].message.content