Source code for langchain_community.utilities.golden_query
"""Util that calls Golden."""importjsonfromtypingimportAny,Dict,Optionalimportrequestsfromlangchain_core.utilsimportget_from_dict_or_envfrompydanticimportBaseModel,ConfigDict,model_validatorGOLDEN_BASE_URL="https://golden.com"GOLDEN_TIMEOUT=5000
[docs]classGoldenQueryAPIWrapper(BaseModel):"""Wrapper for Golden. Docs for using: 1. Go to https://golden.com and sign up for an account 2. Get your API Key from https://golden.com/settings/api 3. Save your API Key into GOLDEN_API_KEY env variable """golden_api_key:Optional[str]=Nonemodel_config=ConfigDict(extra="forbid",)@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:Dict)->Any:"""Validate that api key and python package exists in environment."""golden_api_key=get_from_dict_or_env(values,"golden_api_key","GOLDEN_API_KEY")values["golden_api_key"]=golden_api_keyreturnvalues
[docs]defrun(self,query:str)->str:"""Run query through Golden Query API and return the JSON raw result."""headers={"apikey":self.golden_api_keyor""}response=requests.post(f"{GOLDEN_BASE_URL}/api/v2/public/queries/",json={"prompt":query},headers=headers,timeout=GOLDEN_TIMEOUT,)ifresponse.status_code!=201:returnresponse.textcontent=json.loads(response.content)query_id=content["id"]response=requests.get((f"{GOLDEN_BASE_URL}/api/v2/public/queries/{query_id}/results/""?pageSize=10"),headers=headers,timeout=GOLDEN_TIMEOUT,)returnresponse.text