Source code for langchain_community.utilities.steam
"""Util that calls Steam-WebAPI."""fromtypingimportAny,ListfrompydanticimportBaseModel,ConfigDict,model_validatorfromlangchain_community.tools.steam.promptimport(STEAM_GET_GAMES_DETAILS,STEAM_GET_RECOMMENDED_GAMES,)
[docs]classSteamWebAPIWrapper(BaseModel):"""Wrapper for Steam API."""steam:Any=None# for python-steam-api# operations: a list of dictionaries, each representing a specific operation that# can be performed with the APIoperations:List[dict]=[{"mode":"get_game_details","name":"Get Game Details","description":STEAM_GET_GAMES_DETAILS,},{"mode":"get_recommended_games","name":"Get Recommended Games","description":STEAM_GET_RECOMMENDED_GAMES,},]model_config=ConfigDict(extra="forbid",)
[docs]defget_operations(self)->List[dict]:"""Return a list of operations."""returnself.operations
@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:dict)->Any:"""Validate api key and python package has been configured."""# check if the python package is installedtry:fromsteamimportSteamexceptImportError:raiseImportError("python-steam-api library is not installed. ")try:fromdecoupleimportconfigexceptImportError:raiseImportError("decouple library is not installed. ")# initialize the steam attribute for python-steam-api usageKEY=config("STEAM_KEY")steam=Steam(KEY)values["steam"]=steamreturnvalues
[docs]defparse_to_str(self,details:dict)->str:# For later parsing"""Parse the details result."""result=""forkey,valueindetails.items():result+="The "+str(key)+" is: "+str(value)+"\n"returnresult
[docs]defget_id_link_price(self,games:dict)->dict:"""The response may contain more than one game, so we need to choose the right one and return the id."""game_info={}forappingames["apps"]:game_info["id"]=app["id"]game_info["link"]=app["link"]game_info["price"]=app["price"]breakreturngame_info
[docs]defdetails_of_games(self,name:str)->str:games=self.steam.apps.search_games(name)info_partOne_dict=self.get_id_link_price(games)info_partOne=self.parse_to_str(info_partOne_dict)id=str(info_partOne_dict.get("id"))info_dict=self.steam.apps.get_app_details(id)data=info_dict.get(id).get("data")detailed_description=data.get("detailed_description")# detailed_description contains <li> <br> some other html tags, so we need to# remove themdetailed_description=self.remove_html_tags(detailed_description)supported_languages=info_dict.get(id).get("data").get("supported_languages")info_partTwo=("The summary of the game is: "+detailed_description+"\n"+"The supported languages of the game are: "+supported_languages+"\n")info=info_partOne+info_partTworeturninfo