Allow non streaming

This commit is contained in:
Daniel O'Connell
2023-12-19 22:44:35 +01:00
parent c663939d26
commit 9c808ce89d
2 changed files with 11 additions and 4 deletions
+7 -2
View File
@@ -54,8 +54,10 @@ def chat():
session_id = request.json.get('sessionId')
history = request.json.get('history', [])
settings = request.json.get('settings', {})
followups = request.json.get('followups', True)
as_stream = request.json.get('stream', True)
if query is None:
if query is None and history:
query = history[-1].get('content')
history = history[:-1]
@@ -65,7 +67,10 @@ def chat():
return json.dumps(item)
def run(callback):
return run_query(session_id, query, history, Settings(**settings), callback)
return run_query(session_id, query, history, Settings(**settings), callback, followups)
if not as_stream:
return jsonify(run(None)['text'])
return Response(stream_with_context(stream(stream_callback(run, formatter))), mimetype='text/event-stream')
+4 -2
View File
@@ -226,7 +226,7 @@ def merge_history(history):
return messages
def run_query(session_id: str, query: str, history: List[Dict], settings: Settings, callback: Callable[[Any], None] = None) -> Dict[str, str]:
def run_query(session_id: str, query: str, history: List[Dict], settings: Settings, callback: Callable[[Any], None] = None, followups=True) -> Dict[str, str]:
"""Execute the query.
:param str query: the phrase that was input by the user
@@ -252,7 +252,9 @@ def run_query(session_id: str, query: str, history: List[Dict], settings: Settin
verbose=False,
prompt=make_prompt(settings, chat_model, callbacks),
memory=make_memory(settings, history, callbacks)
) | StampyChain(callbacks=callbacks)
)
if followups:
chain = chain | StampyChain(callbacks=callbacks)
result = chain.invoke({"query": query, 'history': history}, {'callbacks': []})
if callback:
callback({'state': 'done'})