From d085cd16f91e2d79622742f9afcc48fc93639bc3 Mon Sep 17 00:00:00 2001 From: Yura Lukashik Date: Sat, 25 Nov 2023 17:16:23 +0300 Subject: [PATCH] Search: use ChatGPT 3.5 for summaries --- .../external_services/openai.py | 24 +++++++++++++++---- .../semantic_search/load_messages.py | 4 ++-- src/semantic_search/semantic_search/query.py | 4 ++-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/semantic_search/semantic_search/external_services/openai.py b/src/semantic_search/semantic_search/external_services/openai.py index ebb3ff4..e4743f0 100644 --- a/src/semantic_search/semantic_search/external_services/openai.py +++ b/src/semantic_search/semantic_search/external_services/openai.py @@ -22,7 +22,7 @@ def create_embeddings(texts: List[str]) -> List[Any]: @retry(delay=3, backoff=2, tries=8) -def gpt_query_json(query: str) -> str: +def query_chat_gpt_forcing_json(query: str) -> str: summary = openai.ChatCompletion.create( model="gpt-4-1106-preview", messages=[{"role": "user", "content": query}], @@ -32,7 +32,7 @@ def gpt_query_json(query: str) -> str: @retry(delay=3, backoff=2, tries=8) -def gpt_query(query: str) -> str: +def query_chat_gpt(query: str) -> str: summary = openai.ChatCompletion.create( model="gpt-4-1106-preview", messages=[{"role": "user", "content": query}], @@ -41,9 +41,25 @@ def gpt_query(query: str) -> str: @retry(delay=3, backoff=2, tries=8) -def gpt_summarize_thread(thread_messages: List[str]) -> str: +def query_chat_gpt_3_5(query: str) -> str: + summary = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": query}], + ) + return summary.choices[0].message.content.strip() + + +def summarize_thread_with_chat_gpt(thread_messages: List[str]) -> str: + text = "\n".join(thread_messages) + return query_chat_gpt( + "Summarize the following conversation and include channel information and usernames and actual names of the " + "author of the message: " + text + ) + + +def summarize_thread_with_chat_gpt_3_5(thread_messages: List[str]) -> str: text = "\n".join(thread_messages) - return gpt_query( + return query_chat_gpt_3_5( "Summarize the following conversation and include channel information and usernames and actual names of the " "author of the message: " + text ) diff --git a/src/semantic_search/semantic_search/load_messages.py b/src/semantic_search/semantic_search/load_messages.py index 6dee389..10bba2c 100644 --- a/src/semantic_search/semantic_search/load_messages.py +++ b/src/semantic_search/semantic_search/load_messages.py @@ -4,7 +4,7 @@ from .config import CONTEXT_LENGTH from .external_services.pinecone import get_pinecone_index -from .external_services.openai import create_embeddings, gpt_summarize_thread +from .external_services.openai import create_embeddings, summarize_thread_with_chat_gpt_3_5 import datetime from .external_services.slack_api import fetch_thread_messages, fetch_channel_messages, is_thread, \ is_actual_message, \ @@ -152,7 +152,7 @@ def index_messages(channel_id, messages, start_from, pinecone_index, pinecone_na try: logging.info(f" - Summarizing thread {message['thread_ts']}") - summary = gpt_summarize_thread(raw_messages_for_summary) + summary = summarize_thread_with_chat_gpt_3_5(raw_messages_for_summary) messages_for_embedding.append(thread_header.convert_to_summary(summary)) except: logging.info(f" - Failed to summarize - {message['thread_ts']}") diff --git a/src/semantic_search/semantic_search/query.py b/src/semantic_search/semantic_search/query.py index 60f7888..bfa37d5 100644 --- a/src/semantic_search/semantic_search/query.py +++ b/src/semantic_search/semantic_search/query.py @@ -4,7 +4,7 @@ import uuid from datetime import date from .external_services.pinecone import get_pinecone_index -from .external_services.openai import create_embedding, gpt_query_json +from .external_services.openai import create_embedding, query_chat_gpt_forcing_json def build_slack_message_link(workspace_name, channel_id, message_timestamp, thread_timestamp=None): @@ -91,7 +91,7 @@ def smart_query(namespace, query, username: str): gpt_response = None try: - gpt_response = gpt_query_json(prompt) + gpt_response = query_chat_gpt_forcing_json(prompt) result = json.loads(gpt_response) gpt_request_time = time.perf_counter() - gpt_request_start_time total_time = time.perf_counter() - smart_query_start_time