from aimakerspace.text_utils import TextFileLoader, CharacterTextSplitter from aimakerspace.vectordatabase import VectorDatabase import asyncio from aimakerspace.openai_utils.prompts import ( UserRolePrompt, SystemRolePrompt, AssistantRolePrompt, ) from aimakerspace.openai_utils.chatmodel import ChatOpenAI RAG_PROMPT_TEMPLATE = """ \ Use the provided context to answer the user's query. You may not answer the user's query unless there is specific context in the following text. If you do not know the answer, or cannot answer, please respond with "I don't know". """ rag_prompt = SystemRolePrompt(RAG_PROMPT_TEMPLATE) USER_PROMPT_TEMPLATE = """ \ Context: {context} User Query: {user_query} """ user_prompt = UserRolePrompt(USER_PROMPT_TEMPLATE) class RetrievalAugmentedQAPipeline: def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None: self.llm = llm self.vector_db_retriever = vector_db_retriever def run_pipeline(self, user_query: str) -> str: context_list = self.vector_db_retriever.search_by_text(user_query, k=4) context_prompt = "" for context in context_list: context_prompt += context[0] + "\n" formatted_system_prompt = rag_prompt.create_message() formatted_user_prompt = user_prompt.create_message(user_query=user_query, context=context_prompt) return {"response" : self.llm.run([formatted_user_prompt, formatted_system_prompt]), "context" : context_list}