mirror of
https://github.com/wassname/stampy-chat.git
synced 2026-09-12 13:00:42 +08:00
Session ids
This commit is contained in:
+9
-7
@@ -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 -------------
|
||||
|
||||
@@ -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'}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user