Skip to content

Commit

Permalink
Search: use ChatGPT 3.5 for summaries
Browse files Browse the repository at this point in the history
  • Loading branch information
YuraLukashik committed Nov 25, 2023
1 parent 9e597a5 commit d085cd1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
24 changes: 20 additions & 4 deletions src/semantic_search/semantic_search/external_services/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}],
Expand All @@ -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}],
Expand All @@ -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
)
4 changes: 2 additions & 2 deletions src/semantic_search/semantic_search/load_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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']}")
Expand Down
4 changes: 2 additions & 2 deletions src/semantic_search/semantic_search/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d085cd1

Please sign in to comment.