2023-05-31 14:51:23 +03:00
|
|
|
from langchain.embeddings.openai import OpenAIEmbeddings
|
2023-07-11 21:15:56 +03:00
|
|
|
from llm.qa_base import QABaseBrainPicking
|
2023-06-26 11:34:03 +03:00
|
|
|
from logger import get_logger
|
2023-07-04 18:56:54 +03:00
|
|
|
|
2023-06-22 11:45:35 +03:00
|
|
|
logger = get_logger(__name__)
|
2023-06-10 11:43:44 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-07-11 21:15:56 +03:00
|
|
|
class OpenAIBrainPicking(QABaseBrainPicking):
|
2023-06-20 09:56:17 +03:00
|
|
|
"""
|
2023-07-04 18:56:54 +03:00
|
|
|
Main class for the OpenAI Brain Picking functionality.
|
2023-06-20 09:56:17 +03:00
|
|
|
It allows to initialize a Chat model, generate questions and retrieve answers using ConversationalRetrievalChain.
|
|
|
|
"""
|
2023-06-22 11:45:35 +03:00
|
|
|
|
2023-06-20 09:56:17 +03:00
|
|
|
# Default class attributes
|
2023-07-04 18:56:54 +03:00
|
|
|
model: str = "gpt-3.5-turbo"
|
2023-06-22 11:45:35 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
model: str,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id: str,
|
2023-06-26 11:34:03 +03:00
|
|
|
temperature: float,
|
2023-06-22 18:50:06 +03:00
|
|
|
chat_id: str,
|
|
|
|
max_tokens: int,
|
|
|
|
user_openai_api_key: str,
|
2023-06-30 11:10:59 +03:00
|
|
|
streaming: bool = False,
|
2023-07-10 15:27:49 +03:00
|
|
|
) -> "OpenAIBrainPicking": # pyright: ignore reportPrivateUsage=none
|
2023-06-20 09:56:17 +03:00
|
|
|
"""
|
|
|
|
Initialize the BrainPicking class by setting embeddings, supabase client, vector store, language model and chains.
|
2023-07-04 18:56:54 +03:00
|
|
|
:return: OpenAIBrainPicking instance
|
2023-06-20 09:56:17 +03:00
|
|
|
"""
|
2023-06-22 18:50:06 +03:00
|
|
|
super().__init__(
|
|
|
|
model=model,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id=brain_id,
|
2023-06-22 18:50:06 +03:00
|
|
|
chat_id=chat_id,
|
|
|
|
max_tokens=max_tokens,
|
2023-06-26 11:34:03 +03:00
|
|
|
temperature=temperature,
|
2023-06-22 18:50:06 +03:00
|
|
|
user_openai_api_key=user_openai_api_key,
|
2023-07-04 18:56:54 +03:00
|
|
|
streaming=streaming,
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
2023-07-04 18:56:54 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def embeddings(self) -> OpenAIEmbeddings:
|
2023-07-10 15:27:49 +03:00
|
|
|
return OpenAIEmbeddings(
|
|
|
|
openai_api_key=self.openai_api_key
|
|
|
|
) # pyright: ignore reportPrivateUsage=none
|
2023-07-04 18:56:54 +03:00
|
|
|
|
2023-08-07 20:53:04 +03:00
|
|
|
|