add simplified chat API

This commit is contained in:
Fraser
2023-06-12 21:33:11 -04:00
parent 9ed99441e6
commit 97ebac8b07
2 changed files with 28 additions and 5 deletions
+17 -4
View File
@@ -165,24 +165,37 @@ def talk_to_robot_internal(index, query: str, history: List[Dict[str, str]], k:
print('\n' * 10)
print(" ------------------------------ response: -----------------------------")
print(' ------------------------------ response: -----------------------------')
print(response)
log(query)
log(response)
# yield done state, possibly with followup questions
fin_json = {"state": "done"}
fin_json = {'state': 'done'}
followups = multisearch_authored([query, response], DEBUG_PRINT)
for i, followup in enumerate(followups):
fin_json[f"followup_{i}"] = asdict(followup)
fin_json[f'followup_{i}'] = asdict(followup)
yield fin_json
except Exception as e:
print(e)
yield {"state": "error", "error": str(e)}
yield {'state': 'error', 'error': str(e)}
# convert talk_to_robot_internal from dict generator into json generator
def talk_to_robot(index, query: str, history: List[Dict[str, str]], k: int = STANDARD_K, log: Callable = print):
yield from (json.dumps(block) for block in talk_to_robot_internal(index, query, history, k, log))
# wayyy simplified api
def talk_to_robot_simple(index, query: str):
res = {'response': ''}
for block in talk_to_robot_internal(index, query, []):
if block['state'] == 'loading' and block['phase'] == 'semantic' and 'citations' in block:
res['citations'] = block['citations']
elif block['state'] == 'streaming':
res['response'] += block['content']
elif block['state'] == 'error':
res['response'] = block['error']
return json.dumps(res)
+11 -1
View File
@@ -1,7 +1,7 @@
from flask import Flask, jsonify, request, Response
from flask_cors import CORS, cross_origin
from get_blocks import get_top_k_blocks
from chat import talk_to_robot
from chat import talk_to_robot, talk_to_robot_simple
import dataclasses
import os
import openai
@@ -83,6 +83,16 @@ def chat():
return Response(stream(talk_to_robot(PINECONE_INDEX, query, history, log = log)), mimetype='text/event-stream')
# ------------- simplified non-streaming chat for internal testing -------------
@app.route('/chat/<path:param>', methods=['GET'])
@cross_origin()
def chat_simplified(param=''):
return Response(talk_to_robot_simple(PINECONE_INDEX, param))
# ---------------------- human authored content retrieval ----------------------
# act as a proxy, forwarding any requests to /human/<id> to