Package: athina-logger
OverviewAthina Logger is a python SDK to log LLM inferences to Athina API Server.Installationpipinstallathina-loggerUsageimportopenaifromathina_logger.inference_loggerimportInferenceLoggerfromathina_logger.log_stream_inference.openai_chat_completion_streamimportLogOpenAiChatCompletionStreamInferencefromathina_logger.log_stream_inference.openai_completion_streamimportLogOpenAiCompletionStreamInferencefromathina_logger.api_keyimportAthinaApiKeyfromlangchainimportLLMChainfromlangchain.chat_modelsimportChatOpenAIfromlangchain.prompts.chatimport(ChatPromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate,)fromathina_logger.langchain_handlerimportCallbackHandlerfromathina_logger.feedbackimportuser_feedbackimportsseclientimportrequestsimportjsonfromathina_logger.exception.custom_exceptionimportCustomExceptionopenai.api_key='OPENAI_API_KEY'AthinaApiKey.set_api_key('ATHINA_API_KEY')defperformRequestWithStreaming():reqUrl='https://api.openai.com/v1/completions'reqHeaders={'Accept':'text/event-stream','Authorization':'Bearer '+'OPENAI_API_KEY'}reqBody={"model":"text-davinci-003","prompt":"What is Python?","stream":True,}request=requests.post(reqUrl,stream=True,headers=reqHeaders,json=reqBody)logger=LogOpenAiCompletionStreamInference(prompt_slug="test",prompt="What is Python?",model="text-davinci-003",)client=sseclient.SSEClient(request)foreventinclient.events():ifevent.data!='[DONE]':logger.collect_stream_inference_by_chunk(json.loads(event.data))logger.log_stream_inference()defuser_feedback_test():try:user_feedback.UserFeedback.log_user_feedback(external_reference_id="abc",user_feedback=1,user_feedback_comment="test")exceptExceptionase:ifisinstance(e,CustomException):print(e.status_code)print(e.message)else:print(e)defopenai_chat_completion(prompt):response=openai.ChatCompletion.create(model='gpt-3.5-turbo',messages=[{"role":"user","content":prompt}],)try:InferenceLogger.log_open_ai_chat_response(prompt_slug=None,messages=[{"role":"user","content":prompt}],model="gpt-3.5-turbo",completion=response,prompt_response="Hello, How may I assist you?",external_reference_id="abc")exceptExceptionase:ifisinstance(e,CustomException):print(e.status_code)print(e.message)else:print(e)defopenai_completion(prompt):response=openai.Completion.create(model="text-davinci-003",prompt=prompt)InferenceLogger.log_open_ai_completion_response(prompt_slug="test",prompt=prompt,model="text-davinci-003",completion=response,prompt_response="Hello, How may I assist you?",external_reference_id="abc")defgeneric_logging():InferenceLogger.log_generic_response(prompt_slug="test",prompt="Hello, I'm a human.",model="text-davinci-003",prompt_response="Hello, How may I assist you?",prompt_tokens=10,completion_tokens=10,external_reference_id="abc")defstream_openai_chat_response(prompt):response=openai.ChatCompletion.create(model='gpt-3.5-turbo',messages=[{"role":"user","content":prompt}],stream=True,)logger=LogOpenAiChatCompletionStreamInference(prompt_slug="test",messages=[{"role":"user","content":prompt}],model="gpt-3.5-turbo",)forchunkinresponse:logger.collect_stream_inference_by_chunk(chunk)logger.log_stream_inference()defstream_openai_response(prompt):response=openai.Completion.create(model='text-davinci-003',prompt=prompt,max_tokens=1000,stream=True,)logger=LogOpenAiCompletionStreamInference(prompt_slug="test",prompt=prompt,model="text-davinci-003",)logger.collect_stream_inference(response)logger.log_stream_inference()deftest_langchain_streaming():template='''You are a helpful assistant who generates comma separated lists.A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.ONLY return a comma separated list, and nothing more.'''system_message_prompt=SystemMessagePromptTemplate.from_template(template)human_template='{text}'human_message_prompt=HumanMessagePromptTemplate.from_template(human_template)chat_prompt=ChatPromptTemplate.from_messages([system_message_prompt,human_message_prompt])system_template='''You are a helpful assistant who generates lines about a particular topic'''system_message_prompt=SystemMessagePromptTemplate.from_template(system_template)template='''Write few lines on the following topic:{text}Your response:'''chat_prompt=ChatPromptTemplate.from_messages([system_message_prompt,HumanMessagePromptTemplate.from_template(template)])handler=CallbackHandler(prompt_slug='test')# without streamingchain1=LLMChain(llm=ChatOpenAI(openai_api_key='OPENAI_API_KEY',callbacks=[handler]),prompt=chat_prompt,)# with streamingchain2=LLMChain(llm=ChatOpenAI(openai_api_key='OPENAI_API_KEY',streaming=True,callbacks=[handler]),prompt=chat_prompt,)print(chain1.run('India'))print(chain2.run('India'))ContactPlease feel free to reach out toakshat@athina.aiorshiv@athina.aifor more information.