Session ids

This commit is contained in:
Daniel O'Connell
2023-09-29 17:08:26 +02:00
parent a5fdee23ae
commit 382430da32
5 changed files with 34 additions and 18 deletions
+9 -7
View File
@@ -1,9 +1,10 @@
from flask import Flask, jsonify, request, Response
from flask_cors import CORS, cross_origin
from urllib.parse import unquote
import dataclasses
import json
import re
from urllib.parse import unquote
from flask import Flask, jsonify, request, Response
from flask_cors import CORS, cross_origin
from stampy_chat import logging
from stampy_chat.env import PINECONE_INDEX, FLASK_PORT
@@ -42,11 +43,12 @@ def semantic():
@cross_origin()
def chat():
query = request.json['query']
mode = request.json['mode']
history = request.json['history']
query = request.json.get('query')
mode = request.json.get('mode', 'default')
session_id = request.json.get('sessionId')
history = request.json.get('history', [])
return Response(stream(talk_to_robot(PINECONE_INDEX, query, mode, history)), mimetype='text/event-stream')
return Response(stream(talk_to_robot(PINECONE_INDEX, query, mode, history, session_id)), mimetype='text/event-stream')
# ------------- simplified non-streaming chat for internal testing -------------
+9 -3
View File
@@ -156,13 +156,19 @@ def remaining_tokens(prompt: Prompt):
return NUM_TOKENS - used_tokens - TOKENS_BUFFER
def talk_to_robot_internal(index, query: str, mode: str, history: Prompt, k: int = STANDARD_K):
def talk_to_robot_internal(index, query: str, mode: str, history: Prompt, session_id: str, k: int = STANDARD_K):
try:
# 1. Find the most relevant blocks from the Alignment Research Dataset
yield {"state": "loading", "phase": "semantic"}
top_k_blocks = get_top_k_blocks(index, query, k)
yield {"state": "loading", "phase": "semantic", 'citations': [{'title': block.title, 'author': block.authors, 'date': block.date, 'url': block.url} for block in top_k_blocks]}
yield {
"state": "loading", "phase": "semantic",
"citations": [
{'title': block.title, 'author': block.authors, 'date': block.date, 'url': block.url}
for block in top_k_blocks
]
}
# 2. Generate a prompt
yield {"state": "loading", "phase": "prompt"}
@@ -205,7 +211,7 @@ def talk_to_robot_internal(index, query: str, mode: str, history: Prompt, k: int
logger.debug(' ------------------------------ response: -----------------------------')
logger.debug(response)
logger.interaction(query, response, history, prompt, top_k_blocks)
logger.interaction(session_id, query, response, history, prompt, top_k_blocks)
# yield done state, possibly with followup questions
fin_json = {'state': 'done'}
+2 -2
View File
@@ -41,13 +41,13 @@ class ChatLogger(Logger):
def is_debug(self):
return self.isEnabledFor(DEBUG)
def interaction(self, query, response, history, prompt, blocks):
def interaction(self, session_id, query, response, history, prompt, blocks):
prompt = [i for i in prompt if i.get('role') == 'system']
prompt = prompt[0].get('content') if prompt else None
self.item_adder.add(
Interaction(
# session_id=session_id,
session_id=session_id,
interaction_no=len([i for i in history if i.get('role') == 'user']),
query=query,
prompt=prompt,