This commit is contained in:
Alex
2023-09-26 10:03:22 +01:00
parent e5c43cfc4b
commit e85a583f0a
15 changed files with 514 additions and 372 deletions

34
application/llm/openai.py Normal file
View File

@@ -0,0 +1,34 @@
from application.llm.base import BaseLLM
class OpenAILLM(BaseLLM):
def __init__(self, api_key):
global openai
import openai
openai.api_key = api_key
self.api_key = api_key # Save the API key to be used later
def _get_openai(self):
# Import openai when needed
import openai
# Set the API key every time you import openai
openai.api_key = self.api_key
return openai
def gen(self, *args, **kwargs):
# This is just a stub. In the real implementation, you'd hit the OpenAI API or any other service.
return "Non-streaming response from OpenAI."
def gen_stream(self, model, engine, messages, stream=True, **kwargs):
# openai = self._get_openai() # Get the openai module with the API key set
response = openai.ChatCompletion.create(
model=model,
engine=engine,
messages=messages,
stream=stream,
**kwargs
)
for line in response:
if "content" in line["choices"][0]["delta"]:
yield line["choices"][0]["delta"]["content"]