Merge branch 'main' into feature/api-key-create

This commit is contained in:
Alex
2024-03-29 19:59:45 +00:00
committed by GitHub
15 changed files with 340 additions and 107 deletions

View File

@@ -29,12 +29,15 @@ prompts_collection = db["prompts"]
api_key_collection = db["api_keys"]
answer = Blueprint('answer', __name__)
if settings.LLM_NAME == "gpt4":
gpt_model = 'gpt-4'
gpt_model = ""
# to have some kind of default behaviour
if settings.LLM_NAME == "openai":
gpt_model = 'gpt-3.5-turbo'
elif settings.LLM_NAME == "anthropic":
gpt_model = 'claude-2'
else:
gpt_model = 'gpt-3.5-turbo'
if settings.MODEL_NAME: # in case there is particular model name configured
gpt_model = settings.MODEL_NAME
# load the prompts
current_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
@@ -102,9 +105,8 @@ def is_azure_configured():
return settings.OPENAI_API_BASE and settings.OPENAI_API_VERSION and settings.AZURE_DEPLOYMENT_NAME
def complete_stream(question, docsearch, chat_history, prompt_id, conversation_id):
def complete_stream(question, docsearch, chat_history, prompt_id, conversation_id, chunks=2):
llm = LLMCreator.create_llm(settings.LLM_NAME, api_key=settings.API_KEY)
if prompt_id == 'default':
prompt = chat_combine_template
elif prompt_id == 'creative':
@@ -113,8 +115,11 @@ def complete_stream(question, docsearch, chat_history, prompt_id, conversation_i
prompt = chat_combine_strict
else:
prompt = prompts_collection.find_one({"_id": ObjectId(prompt_id)})["content"]
docs = docsearch.search(question, k=2)
if chunks == 0:
docs = []
else:
docs = docsearch.search(question, k=chunks)
if settings.LLM_NAME == "llama.cpp":
docs = [docs[0]]
# join all page_content together with a newline
@@ -202,6 +207,10 @@ def stream():
prompt_id = data["prompt_id"]
else:
prompt_id = 'default'
if 'chunks' in data:
chunks = int(data["chunks"])
else:
chunks = 2
# check if active_docs is set
@@ -218,7 +227,8 @@ def stream():
complete_stream(question, docsearch,
chat_history=history,
prompt_id=prompt_id,
conversation_id=conversation_id), mimetype="text/event-stream"
conversation_id=conversation_id,
chunks=chunks), mimetype="text/event-stream"
)
@@ -239,6 +249,10 @@ def api_answer():
prompt_id = data["prompt_id"]
else:
prompt_id = 'default'
if 'chunks' in data:
chunks = int(data["chunks"])
else:
chunks = 2
if prompt_id == 'default':
prompt = chat_combine_template
@@ -266,7 +280,10 @@ def api_answer():
docs = docsearch.search(question, k=2)
if chunks == 0:
docs = []
else:
docs = docsearch.search(question, k=chunks)
# join all page_content together with a newline
docs_together = "\n".join([doc.page_content for doc in docs])
p_chat_combine = prompt.replace("{summaries}", docs_together)
@@ -361,9 +378,15 @@ def api_search():
vectorstore = get_vectorstore({"active_docs": data["active_docs"]})
else:
vectorstore = ""
if 'chunks' in data:
chunks = int(data["chunks"])
else:
chunks = 2
docsearch = VectorCreator.create_vectorstore(settings.VECTOR_STORE, vectorstore, settings.EMBEDDINGS_KEY)
docs = docsearch.search(question, k=2)
if chunks == 0:
docs = []
else:
docs = docsearch.search(question, k=chunks)
source_log_docs = []
for doc in docs: