Spaces:
Sleeping
Sleeping
from langchain_core.prompts import ChatPromptTemplate | |
from langchain_core.output_parsers import StrOutputParser | |
from helpers.generate_embbedings import vector_store | |
from helpers.f5_model import f5_model | |
def make_prompt(history, prompt, context=None): | |
formatted_history = "" | |
if context: | |
formatted_history += f"[CONTEXT] {context} [/CONTEXT]\n" | |
for history_item in history: | |
if history_item.from_ == 'user': | |
formatted_history += f"[INST] {history_item.message} [/INST]\n" | |
else: | |
formatted_history += f"{history_item.message}\n" | |
formatted_history += f"[INST] {prompt} [/INST]\n" | |
return formatted_history | |
async def ask_question(question: str, history: list = [], project_id=None): | |
""" | |
Generate a response using F5 model based on history and project-specific context. | |
""" | |
try: | |
context = "" | |
if project_id is not None: | |
context = vector_store.similarity_search( | |
query=question, k=4, filter={"project_id": project_id} | |
) | |
prompt = make_prompt(history, question, context) | |
async for chunk in f5_model.stream_response(prompt): | |
yield chunk | |
except Exception as e: | |
raise RuntimeError(f"Error generating response: {str(e)}") | |