LiKenun commited on
Commit
cf871ae
·
1 Parent(s): f7e11c1

Put inquirer name in question context

Browse files
src/ctp_slack_bot/db/repositories/mongo_db_vectorized_chunk_repository.py CHANGED
@@ -40,23 +40,6 @@ class MongoVectorizedChunkRepository(VectorRepositoryBase, VectorizedChunkReposi
40
  document = await self.collection.find_one({"parent_id": parent_id, "chunk_id": chunk_id})
41
  return VectorizedChunk(**document) if document else None
42
 
43
- async def find_by_vector(self: Self, query_embedding: Sequence[float], k: int = 5, score_threshold: float = 0.7) -> Sequence[VectorizedChunk]:
44
- pipeline = [
45
- {
46
- "$vectorSearch": {
47
- "index": "vector_index",
48
- "path": "embedding",
49
- "queryVector": query_embedding,
50
- "numCandidates": k * 2,
51
- "limit": k,
52
- "score": {"$meta": "vectorSearchScore"}
53
- }
54
- },
55
- {"$match": {"score": {"$gte": score_threshold}}}
56
- ]
57
- cursor = self.collection.aggregate(pipeline)
58
- return [VectorizedChunk(**document) async for document in cursor] # TODO: mutable until async support is extended to tuples
59
-
60
  async def find_by_vector(self: Self, query: VectorQuery) -> Sequence[Chunk]:
61
  """
62
  Query the vector database for similar documents.
 
40
  document = await self.collection.find_one({"parent_id": parent_id, "chunk_id": chunk_id})
41
  return VectorizedChunk(**document) if document else None
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  async def find_by_vector(self: Self, query: VectorQuery) -> Sequence[Chunk]:
44
  """
45
  Query the vector database for similar documents.
src/ctp_slack_bot/services/answer_retrieval_service.py CHANGED
@@ -28,7 +28,7 @@ class AnswerRetrievalService(BaseModel):
28
  async def push(self: Self, question: SlackMessage, context: Collection[Chunk]) -> None:
29
  channel_to_respond_to = question.channel
30
  thread_to_respond_to = question.thread_ts if question.thread_ts else question.ts
31
- answer = await self.language_model_service.answer_question(question.text, context)
32
  logger.debug("Pushing response to channel {} and thread {}: {}", channel_to_respond_to, thread_to_respond_to, answer)
33
  slack_response = SlackResponse(text=answer, channel=channel_to_respond_to, thread_ts=thread_to_respond_to)
34
  await self.event_brokerage_service.publish(EventType.OUTGOING_SLACK_RESPONSE, slack_response)
 
28
  async def push(self: Self, question: SlackMessage, context: Collection[Chunk]) -> None:
29
  channel_to_respond_to = question.channel
30
  thread_to_respond_to = question.thread_ts if question.thread_ts else question.ts
31
+ answer = await self.language_model_service.answer_question(question.user, question.text, context)
32
  logger.debug("Pushing response to channel {} and thread {}: {}", channel_to_respond_to, thread_to_respond_to, answer)
33
  slack_response = SlackResponse(text=answer, channel=channel_to_respond_to, thread_ts=thread_to_respond_to)
34
  await self.event_brokerage_service.publish(EventType.OUTGOING_SLACK_RESPONSE, slack_response)
src/ctp_slack_bot/services/language_model_service.py CHANGED
@@ -23,7 +23,7 @@ class LanguageModelService(BaseModel):
23
  self._open_ai_client = AsyncOpenAI(api_key=self.settings.OPENAI_API_KEY.get_secret_value())
24
  logger.debug("Created {}", self.__class__.__name__)
25
 
26
- async def answer_question(self, question: str, context: Collection[Chunk]) -> str:
27
  """Generate a response using OpenAI’s API with retrieved context.
28
 
29
  Args:
@@ -37,13 +37,16 @@ class LanguageModelService(BaseModel):
37
  messages = [
38
  {"role": "system", "content": self.settings.SYSTEM_PROMPT},
39
  {"role": "user", "content":
40
- f"""Student Question:
 
 
 
41
  {question}
42
 
43
  Context from class materials and transcripts:
44
  {'\n\n'.join(chunk.text for chunk in context)}
45
 
46
- Please answer the Student Question based on the Context from class materials and transcripts. If the context doesn’t contain relevant information, acknowledge that and suggest asking the professor. Otherwise, carry on."""}
47
  ]
48
  response: ChatCompletion = await self._open_ai_client.chat.completions.create(
49
  model=self.settings.CHAT_MODEL,
 
23
  self._open_ai_client = AsyncOpenAI(api_key=self.settings.OPENAI_API_KEY.get_secret_value())
24
  logger.debug("Created {}", self.__class__.__name__)
25
 
26
+ async def answer_question(self, asker: str, question: str, context: Collection[Chunk]) -> str:
27
  """Generate a response using OpenAI’s API with retrieved context.
28
 
29
  Args:
 
37
  messages = [
38
  {"role": "system", "content": self.settings.SYSTEM_PROMPT},
39
  {"role": "user", "content":
40
+ f"""
41
+ Inquirer Name: {asker}
42
+
43
+ Question:
44
  {question}
45
 
46
  Context from class materials and transcripts:
47
  {'\n\n'.join(chunk.text for chunk in context)}
48
 
49
+ Please answer the Question based on the Context from class materials and transcripts. If the context doesn’t contain relevant information, acknowledge that and suggest asking the professor. In all other cases, carry on."""}
50
  ]
51
  response: ChatCompletion = await self._open_ai_client.chat.completions.create(
52
  model=self.settings.CHAT_MODEL,
src/ctp_slack_bot/services/slack_service.py CHANGED
@@ -35,12 +35,13 @@ class SlackService(BaseModel):
35
 
36
  def adapt_event_payload(self: Self, event: Mapping[str, Any]) -> SlackMessage:
37
  text = SLACK_USER_MENTION_PATTERN.sub(lambda match: f"@{self.user_id_name_map.get(match.group(1))}", event.get("text", "")) # TODO: permit look-up of Slack again when not found.
 
38
  return SlackMessage(
39
  type=event.get("type"),
40
  subtype=event.get("subtype"),
41
  channel=event.get("channel"),
42
  channel_type=event.get("channel_type"),
43
- user=event.get("user"),
44
  bot_id=event.get("bot_id"),
45
  thread_ts=event.get("thread_ts"),
46
  text=text,
 
35
 
36
  def adapt_event_payload(self: Self, event: Mapping[str, Any]) -> SlackMessage:
37
  text = SLACK_USER_MENTION_PATTERN.sub(lambda match: f"@{self.user_id_name_map.get(match.group(1))}", event.get("text", "")) # TODO: permit look-up of Slack again when not found.
38
+ user_id = event.get("user")
39
  return SlackMessage(
40
  type=event.get("type"),
41
  subtype=event.get("subtype"),
42
  channel=event.get("channel"),
43
  channel_type=event.get("channel_type"),
44
+ user=self.user_id_name_map.get(user_id, user_id),
45
  bot_id=event.get("bot_id"),
46
  thread_ts=event.get("thread_ts"),
47
  text=text,