From fd0cbfc1b9c4fa2fd9f1b691b330af9c108eba4b Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Fri, 29 Sep 2023 14:03:50 +0200 Subject: [PATCH 1/4] const for max log message length --- api/src/stampy_chat/logging.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api/src/stampy_chat/logging.py b/api/src/stampy_chat/logging.py index 67254af..c58c827 100644 --- a/api/src/stampy_chat/logging.py +++ b/api/src/stampy_chat/logging.py @@ -6,6 +6,8 @@ from stampy_chat.db.session import ItemAdder from stampy_chat.db.models import Interaction +MAX_MESSAGE_LEN = 2000 - 8 + class DiscordHandler(StreamHandler): def emit(self, record): # Ignore messages that come from non chat modules @@ -22,8 +24,8 @@ class DiscordHandler(StreamHandler): if not DISCORD_LOGGING_URL: return - while len(message) > 2000 - 8: - m_section, message = message[:2000 - 8], message[2000 - 8:] + while len(message) > MAX_MESSAGE_LEN: + m_section, message = message[:MAX_MESSAGE_LEN], message[MAX_MESSAGE_LEN:] m_section = "```\n" + m_section + "\n```" DiscordWebhook(url=DISCORD_LOGGING_URL, content=m_section).execute() DiscordWebhook(url=DISCORD_LOGGING_URL, content="```\n" + message + "\n```").execute() From 677f3ba26400ed3e40bdf7ea82936abafb52791f Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Fri, 29 Sep 2023 14:04:20 +0200 Subject: [PATCH 2/4] Handle both old and new pinecode sturctures --- api/src/stampy_chat/get_blocks.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/api/src/stampy_chat/get_blocks.py b/api/src/stampy_chat/get_blocks.py index 0119ccb..a5a68ed 100644 --- a/api/src/stampy_chat/get_blocks.py +++ b/api/src/stampy_chat/get_blocks.py @@ -93,8 +93,9 @@ def get_top_k_blocks(index, user_query: str, k: int) -> List[Block]: ) blocks = [] for match in query_response['matches']: + metadata = match['metadata'] - date = match['metadata']['date_published'] + date = metadata.get('date_published') or metadata.get('date') if isinstance(date, datetime.date): date = date.isoformat() @@ -103,18 +104,18 @@ def get_top_k_blocks(index, user_query: str, k: int) -> List[Block]: elif isinstance(date, float): date = datetime.datetime.fromtimestamp(date).date().isoformat() - authors = match['metadata'].get('authors') - if not authors and match['metadata'].get('author'): - authors = [match['metadata'].get('author')] + authors = metadata.get('authors') + if not authors and metadata.get('author'): + authors = [metadata.get('author')] blocks.append(Block( - id = match['metadata']['hash_id'], - title = match['metadata']['title'], + id = metadata.get('hash_id'), + title = metadata['title'], authors = authors, date = date, - url = match['metadata']['url'], - tags = match['metadata'].get('tags'), - text = strip_block(match['metadata']['text']) + url = metadata['url'], + tags = metadata.get('tags'), + text = strip_block(metadata['text']) )) t2 = time.time() From afacc9e3f570691bfca328141f02aa4186f3fc14 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Fri, 29 Sep 2023 14:12:07 +0200 Subject: [PATCH 3/4] Env param for remote chat instance --- api/src/stampy_chat/env.py | 3 +++ api/src/stampy_chat/get_blocks.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/api/src/stampy_chat/env.py b/api/src/stampy_chat/env.py index 4bc2952..37b7389 100644 --- a/api/src/stampy_chat/env.py +++ b/api/src/stampy_chat/env.py @@ -41,3 +41,6 @@ host = os.environ.get("CHAT_DB_HOST", "127.0.0.1") port = os.environ.get("CHAT_DB_PORT", "3306") db_name = os.environ.get("CHAT_DB_NAME", "stampy_chat") DB_CONNECTION_URI = f"mysql+mysqlconnector://{user}:{password}@{host}:{port}/{db_name}" + +### Local testing helpers ### +REMOTE_CHAT_INSTANCE = os.environ.get("REMOTE_CHAT_INSTANCE", "https://chat.stampy.ai:8443") diff --git a/api/src/stampy_chat/get_blocks.py b/api/src/stampy_chat/get_blocks.py index a5a68ed..e09b71c 100644 --- a/api/src/stampy_chat/get_blocks.py +++ b/api/src/stampy_chat/get_blocks.py @@ -7,7 +7,7 @@ import regex as re import requests import time from typing import List, Tuple -from stampy_chat.env import PINECONE_NAMESPACE +from stampy_chat.env import PINECONE_NAMESPACE, REMOTE_CHAT_INSTANCE from stampy_chat import logging @@ -66,7 +66,7 @@ def get_top_k_blocks(index, user_query: str, k: int) -> List[Block]: logger.info('Pinecone index not found, performing semantic search on chat.stampy.ai endpoint.') response = requests.post( - "https://chat.stampy.ai:8443/semantic", + REMOTE_CHAT_INSTANCE, json = { "query": user_query, "k": k From 0bef69b14071079ba8f073f6ac60e301f0f2db99 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Fri, 29 Sep 2023 14:48:09 +0200 Subject: [PATCH 4/4] Models configured via env variables --- api/src/stampy_chat/chat.py | 24 +++++++++--------------- api/src/stampy_chat/env.py | 4 ++++ api/src/stampy_chat/get_blocks.py | 6 +----- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/api/src/stampy_chat/chat.py b/api/src/stampy_chat/chat.py index a2c7cf6..59a4845 100644 --- a/api/src/stampy_chat/chat.py +++ b/api/src/stampy_chat/chat.py @@ -1,12 +1,14 @@ -# ------------------------------- env, constants ------------------------------- -from dataclasses import asdict -from typing import List, Dict, Callable -import openai -import re -from sqlalchemy.orm import PropComparator -import tiktoken import time +import json +import re +import time +from dataclasses import asdict +from typing import List, Dict +import openai +import tiktoken + +from stampy_chat.env import COMPLETIONS_MODEL from stampy_chat.followups import multisearch_authored from stampy_chat.get_blocks import get_top_k_blocks, Block from stampy_chat import logging @@ -15,11 +17,6 @@ from stampy_chat import logging logger = logging.getLogger(__name__) -# OpenAI models -EMBEDDING_MODEL = "text-embedding-ada-002" -COMPLETIONS_MODEL = "gpt-3.5-turbo" -# COMPLETIONS_MODEL = "gpt-4" - STANDARD_K = 20 if COMPLETIONS_MODEL == 'gpt-4' else 10 # parameters @@ -139,9 +136,6 @@ def construct_prompt(query: str, mode: str, history: Prompt, context: List[Block return prompt # ------------------------------- completion code ------------------------------- -import time -import json - def check_openai_moderation(prompt: Prompt, query: str): prompt_string = '\n\n'.join([message["content"] for message in prompt]) diff --git a/api/src/stampy_chat/env.py b/api/src/stampy_chat/env.py index 37b7389..51e31ec 100644 --- a/api/src/stampy_chat/env.py +++ b/api/src/stampy_chat/env.py @@ -18,6 +18,10 @@ DISCORD_LOGGING_URL = os.environ.get('LOGGING_URL') OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') openai.api_key = OPENAI_API_KEY # non-optional +### Models ### +EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "text-embedding-ada-002") +COMPLETIONS_MODEL = os.environ.get("COMPLETIONS_MODEL", "gpt-3.5-turbo") + ### Pinecone ### PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY') PINECONE_ENVIRONMENT = os.environ.get('PINECONE_ENVIRONMENT', "us-east1-gcp") diff --git a/api/src/stampy_chat/get_blocks.py b/api/src/stampy_chat/get_blocks.py index e09b71c..0908132 100644 --- a/api/src/stampy_chat/get_blocks.py +++ b/api/src/stampy_chat/get_blocks.py @@ -7,16 +7,12 @@ import regex as re import requests import time from typing import List, Tuple -from stampy_chat.env import PINECONE_NAMESPACE, REMOTE_CHAT_INSTANCE +from stampy_chat.env import PINECONE_NAMESPACE, REMOTE_CHAT_INSTANCE, EMBEDDING_MODEL from stampy_chat import logging logger = logging.getLogger(__name__) -# ---------------------------------- constants --------------------------------- - -EMBEDDING_MODEL = "text-embedding-ada-002" - # ------------------------------------ types ----------------------------------- @dataclasses.dataclass