-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f216cc8
commit c9d5cea
Showing
17 changed files
with
265 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .cricbot_chain import generate_chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from src.services import IntentHandlerService, ResponseGeneratorService, LiveMatchService, IntentIdentifierService | ||
from src.utils import get_live_matches_as_string | ||
from src.models import IntentDetails | ||
from langchain_core.output_parsers import JsonOutputParser, StrOutputParser | ||
|
||
def generate_chain(openai_api_key: str, metadata: dict): | ||
intent_identifier_service = IntentIdentifierService(openai_api_key) | ||
response_generator_service = ResponseGeneratorService(openai_api_key) | ||
intent_handler_service = IntentHandlerService() | ||
json_parser = JsonOutputParser(pydantic_object=IntentDetails) | ||
str_parser = StrOutputParser() | ||
chain = (lambda x: {**metadata, "live_matches": get_live_matches_as_string(LiveMatchService().fetch_all_matches())}) \ | ||
| intent_identifier_service.get_chat_prompt_template(json_parser) \ | ||
| intent_identifier_service.llm \ | ||
| json_parser \ | ||
| intent_handler_service.get_addtional_data \ | ||
| (lambda data: {**metadata, **data}) \ | ||
| response_generator_service.get_prompt \ | ||
| response_generator_service.llm \ | ||
| str_parser | ||
return chain | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .intents import Intent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from enum import Enum | ||
|
||
|
||
class Intent(str, Enum): | ||
live_matches = "live_matches" | ||
live_score = "live_score" | ||
fallback = "fallback" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .match_details import MatchDetails, TeamScoreDetails | ||
from .match_details import MatchDetails, TeamScoreDetails | ||
from .intent_details import IntentDetails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from datetime import datetime | ||
from typing import Optional | ||
from pydantic import BaseModel, Field | ||
|
||
from src.enums import Intent | ||
|
||
class Entities(BaseModel): | ||
series: Optional[str] = Field(None, description="Series of a cricket match") | ||
team1: Optional[str] = Field(None, description="Name of team 1") | ||
team2: Optional[str] = Field(None, description="Name of team 2") | ||
reason: Optional[str] = Field(None, description="Reason why intent identification failed") | ||
date: Optional[datetime] = Field(None, description="Date of the match") | ||
|
||
class IntentDetails(BaseModel): | ||
intent: Intent = Field(description="intent of the text message") | ||
entities: Optional[Entities] = Field(default_factory=Entities, description="Entities to find in the text message") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Role: | ||
You are an expert in classifying intent and identifying entities from a plain text. | ||
|
||
Context: | ||
We are building a chatbot about Cricket where you need to find intent and entities in the message. | ||
Following are the list of live matches: | ||
{live_matches} | ||
|
||
Tasks: | ||
- Identify the intent and entities in the given text. Possible intents and their corresponding entities are: | ||
# 'live_matches': User is trying to find the list of all live matches. Try to identify the series name from the text based on above list: Entity to find is: | ||
# 'series' - Series from above list [Optional] | ||
# 'live_score': User is trying to find the live score of a cricket match between 2 teams. Check above list of live matches and identify the teams from the text. If you are not able to identify the teams, then return 'live_matches' intent. If teams are found, then return entities as: | ||
* 'team1' - Cricket team 1 [Mandatory] | ||
* 'team2' - Cricket team 2 [Mandatory] | ||
# 'fallback': If text doesn't fit in any of the above intents, then return this intent. Entity to find is: | ||
* 'reason' - output the reason because of which you are not able to indetify the intent in the given text. | ||
- {format_instructions} | ||
- Consider edge cases like multiple entities in a message or unclear intents and provide reasonable interpretations. | ||
- Ensure all outputs are contextually accurate and specific to Cricket. | ||
|
||
Example1: | ||
Input: Get me live scores of cricket match between india and australia. | ||
Output: {{ | ||
"intent": "live_score", | ||
"entities": {{ | ||
"team1": "india", | ||
"team2": "australia" | ||
}} | ||
}} | ||
|
||
Example2: | ||
Input: mumbai indians vs gujarat titans | ||
Output: {{ | ||
"intent": "live_score", | ||
"entities": {{ | ||
"team1": "mumbai indians", | ||
"team2": "gujarat titans" | ||
}} | ||
}} | ||
|
||
Example3: | ||
Input: Show me live score of football match | ||
Output: {{ | ||
"intent": "fallback" | ||
"entities": {{ | ||
"reason": "Cannot show live score of a football match." | ||
}} | ||
}} | ||
|
||
Example4: | ||
Input: xyz vs abcd | ||
Output: {{ | ||
"intent": "live_matches", | ||
"entities": {{}} | ||
}} | ||
|
||
Example5: | ||
Input: List all the matches of india vs bangladesh series. | ||
Output: {{ | ||
"intent": "live_matches", | ||
"entities": {{ | ||
"series": "india-vs-bangladesh" | ||
}} | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
from .cricbot_service import CricbotService | ||
from .cricbot_service import CricbotService | ||
from .intent_identifier_service import IntentIdentifierService | ||
from .live_match_service import LiveMatchService | ||
from .response_generator_service import ResponseGeneratorService | ||
from .intent_handler_service import IntentHandlerService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from .live_match_service import LiveMatchService | ||
from src.constants import Constants | ||
from src.enums import Intent | ||
from src.models import IntentDetails | ||
|
||
class IntentHandlerService: | ||
|
||
def __init__(self): | ||
self.__live_match_service = LiveMatchService() | ||
|
||
def get_addtional_data(self, data: dict) -> dict: | ||
intent_details = IntentDetails(**data) | ||
match intent_details.intent: | ||
case Intent.live_matches: | ||
additional_data = self.__get_current_matches_intent_data(intent_details) | ||
case Intent.live_score: | ||
additional_data = self.__get_live_score_intent_data(intent_details) | ||
case _: | ||
additional_data = self.__get_fallback_intent_data(intent_details) | ||
return {**data, **additional_data} | ||
|
||
def __get_current_matches_intent_data(self, intent_details: IntentDetails) -> dict: | ||
entities = intent_details.entities | ||
live_matches = self.__live_match_service.fetch_all_matches(entities.date) | ||
series = entities.series | ||
if series: | ||
live_matches_of_series = [match for match in live_matches if match.series_name.lower() == series.lower()] | ||
additional_data = { | ||
"series": series, | ||
"live_matches": live_matches_of_series | ||
} | ||
else: | ||
additional_data = { | ||
"live_matches": live_matches | ||
} | ||
return additional_data | ||
|
||
def __get_live_score_intent_data(self, intent_details: IntentDetails) -> dict: | ||
entities = intent_details.entities | ||
match_score, live_matches = self.__live_match_service.fetch_live_score( | ||
entities.team1, | ||
entities.team2 | ||
) | ||
if match_score is None and len(live_matches) > 0: | ||
additional_data = { | ||
"intent": Intent.live_matches, | ||
"entities": {}, | ||
"live_matches": live_matches | ||
} | ||
elif match_score is None: | ||
additional_data = { | ||
"intent": Intent.fallback, | ||
"entities": { | ||
"reason": Constants.MATCHES_NOT_PRESENT_REASON | ||
} | ||
} | ||
else: | ||
additional_data = { | ||
"match_score": match_score | ||
} | ||
return additional_data | ||
|
||
def __get_fallback_intent_data(self, intent_details: IntentDetails) -> dict: | ||
entities = intent_details.entities | ||
if not entities.reason: | ||
return { | ||
"entities": { | ||
"reason": Constants.REASON_NOT_PRESENT | ||
} | ||
} | ||
return {} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.