Package: reconify
Reconify PIP moduleThe Reconify module is used for sending data to the Reconify platform atwww.reconify.com.Currently the module supports processing and analyzing Chats, Completions, and Images from:OpenAIAmazon Bedrock(Amazon Titan, AI21 Jurassic, Anthropic Claude, Cohere Command, Meta LLama 2, and Stability Stable Diffusion)AnthropicCohereMistralPerplexitySupport for additional actions and providers will be added.Get startedThe first step is to create an account atapp.reconify.com.Generate API and APP KeysIn the Reconify console, add an Application to your account. This will generate both an API_KEY and an APP_KEY
which will be used in the code below to send data to Reconify.Install the modulepip install reconifyIntegrate the module with OpenAIThe following instructions are for OpenAI's Python SDK v1 or later (released in Nov 2023). For earlier versions of OpenAI's SDK, follow thelegacy instructionsImport the modulefromreconifyimportreconifyOpenAIHandlerInitialize the modulePrior to initializing the Reconify module, make sure to import the OpenaAI module.fromopenaiimportOpenAIopenai_client=OpenAI(api_key='YOUR_OPENAI_KEY')Configure the instance of Reconify passing the OpenAi instance along with the Reconify API_KEY and APP_KEY created above.reconifyOpenAIHandler.config(openai_client,appKey='Your_App_Key',apiKey='Your_Api_Key')This is all that is needed for a basic integration. The module takes care of sending the correct data to Reconify when you call openai_client.completions.create, openai_client.chat.completions.create, openai_client.images.generate.Optional Config ParametersThere are additional optional parameters that can be passed in to the handler.debug: (default False) Enable/Disable console loggingtrackImages: (default True) Turn on/off tracking of createImageFor example:reconifyOpenAIHandler.config(openai_client,appKey='Your_App_Key',apiKey='Your_Api_Key',debug=True)Optional methodsYou can optionally pass in a user object or session ID to be used in the analytics reporting.
The session ID will be used to group interactions together in the same session transcript.Set a userThe user JSON should include a unique userId, all the other fields are optional.
Without a unique userId, each user will be treated as a new user.reconifyOpenAIHandler.setUser({"userId":"ABC123","isAuthenticated":1,"firstName":"Francis","lastName":"Smith","email":"","phone":"","gender":"female"})Set a Session IDThe Session ID is an alphanumeric string.reconifyOpenAIHandler.setSession('MySessionId')Set Session TimeoutSet the session timeout in minutes to override the defaultreconifyOpenAIHandler.setSessionTimeout(15)SeeExamples with OpenAIIntegrate the module with Amazon Bedrock RuntimeImport the modulefromreconifyimportreconifyBedrockRuntimeHandlerInitialize the modulePrior to initializing the Reconify module, make sure to import the Amazon boto3 module.importboto3bedrock=boto3.client('bedrock-runtime')Configure the instance of Reconify passing the Bedrock Runtime instance along with the Reconify API_KEY and APP_KEY created above.reconifyBedrockRuntimeHandler.config(bedrock,appKey='Your_App_Key',apiKey='Your_Api_Key')This is all that is needed for a basic integration. The module takes care of sending the correct data to Reconify when you call bedrock.invoke_model().Response handlingWhen using the Reconify module, the response body frominvoke_modelwill be converted frombotocore.response.StreamingBodyto JSON and saved in the response asparsedBody. See the examples below for more info.Optional Config ParametersThere are additional optional parameters that can be passed in to the handler.debug: (default False) Enable/Disable console loggingtrackImages: (default True) Turn on/off tracking of createImageFor example:reconifyBedrockRuntimeHandler.config(bedrock,appKey='Your_App_Key',apiKey='Your_Api_Key',debug=True)Optional methodsYou can optionally pass in a user object or session ID to be used in the analytics reporting.
The session ID will be used to group interactions together in the same session transcript.Set a userThe user JSON should include a unique userId, all the other fields are optional.
Without a unique userId, each user will be treated as a new user.reconifyBedrockRuntimeHandler.setUser({"userId":"ABC123","firstName":"Francis","lastName":"Smith"})Set a Session IDThe Session ID is an alphanumeric string.reconifyBedrockRuntimeHandler.setSession('MySessionId')Set Session TimeoutSet the session timeout in minutes to override the defaultreconifyBedrockRuntimeHandler.setSessionTimeout(15)SeeExamples with Amazon Bedrock RuntimeIntegrate the module with AnthropicThe following instructions are for Anthropic's Python SDK.Import the modulefromreconifyimportreconifyAnthropicHandlerInitialize the modulePrior to initializing the Reconify module, make sure to import the Anthropic module.fromanthropicimportAnthropic,HUMAN_PROMPT,AI_PROMPTanthropic=Anthropic(api_key='YOUR_ANTHROPIC_KEY')Configure the instance of Reconify passing the Anthropic instance along with the Reconify API_KEY and APP_KEY created above.reconifyAnthropicHandler.config(anthropic,appKey='Your_App_Key',apiKey='Your_Api_Key')This is all that is needed for a basic integration. The module takes care of sending the correct data to Reconify.Optional Config ParametersThere are additional optional parameters that can be passed in to the handler.debug: (default False) Enable/Disable console loggingtrackImages: (default True) Turn on/off tracking of createImageFor example:reconifyAnthropicHandler.config(anthropic,appKey='Your_App_Key',apiKey='Your_Api_Key',debug=True)Optional methodsYou can optionally pass in a user object or session ID to be used in the analytics reporting.
The session ID will be used to group interactions together in the same session transcript.Set a userThe user JSON should include a unique userId, all the other fields are optional.
Without a unique userId, each user will be treated as a new user.reconifyAnthropicHandler.setUser({"userId":"ABC123","firstName":"Francis","lastName":"Smith"})Set a Session IDThe Session ID is an alphanumeric string.reconifyAnthropicHandler.setSession('MySessionId')Set Session TimeoutSet the session timeout in minutes to override the defaultreconifyAnthropicHandler.setSessionTimeout(15)SeeExamples with AnthropicIntegrate the module with CohereThe following instructions are for Cohere's Python SDK.Import the modulefromreconifyimportreconifyCohereHandlerInitialize the modulePrior to initializing the Reconify module, make sure to import the Cohere module.fromcohereimportClientcohere_client=Client('YOUR_COHERE_KEY')Configure the instance of Reconify passing the Cohere instance along with the Reconify API_KEY and APP_KEY created above.reconifyCohereHandler.config(cohere_client,appKey='Your_App_Key',apiKey='Your_Api_Key')This is all that is needed for a basic integration. The module takes care of sending the correct data to Reconify.Optional Config ParametersThere are additional optional parameters that can be passed in to the handler.debug: (default False) Enable/Disable console loggingtrackImages: (default True) Turn on/off tracking of createImageFor example:reconifyCohereHandler.config(cohere_client,appKey='Your_App_Key',apiKey='Your_Api_Key',debug=True)Optional methodsYou can optionally pass in a user object or session ID to be used in the analytics reporting.
The session ID will be used to group interactions together in the same session transcript.Set a userThe user JSON should include a unique userId, all the other fields are optional.
Without a unique userId, each user will be treated as a new user.reconifyCohereHandler.setUser({"userId":"ABC123","firstName":"Francis","lastName":"Smith"})Set a Session IDThe Session ID is an alphanumeric string.reconifyCohereHandler.setSession('MySessionId')Set Session TimeoutSet the session timeout in minutes to override the defaultreconifyCohereHandler.setSessionTimeout(15)SeeExamples with CohereIntegrate the module with MistralThe following instructions are for Mistral's Python SDK.Import the modulefromreconifyimportreconifyMistralHandlerInitialize the modulePrior to initializing the Reconify module, make sure to import the Mistral module.frommistralai.clientimportMistralClientfrommistralai.models.chat_completionimportChatMessageclient=MistralClient(api_key='YOUR_MISTRAL_KEY')Configure the instance of Reconify passing the Mistral instance along with the Reconify API_KEY and APP_KEY created above.reconifyMistralHandler.config(client,appKey='Your_App_Key',apiKey='Your_Api_Key')This is all that is needed for a basic integration. The module takes care of sending the correct data to Reconify.Optional Config ParametersThere are additional optional parameters that can be passed in to the handler.debug: (default False) Enable/Disable console loggingtrackImages: (default True) Turn on/off tracking of createImageFor example:reconifyMistralHandler.config(client,appKey='Your_App_Key',apiKey='Your_Api_Key',debug=True)Optional methodsYou can optionally pass in a user object or session ID to be used in the analytics reporting.
The session ID will be used to group interactions together in the same session transcript.Set a userThe user JSON should include a unique userId, all the other fields are optional.
Without a unique userId, each user will be treated as a new user.reconifyMistralHandler.setUser({"userId":"ABC123","firstName":"Francis","lastName":"Smith"})Set a Session IDThe Session ID is an alphanumeric string.reconifyMistralHandler.setSession('MySessionId')Set Session TimeoutSet the session timeout in minutes to override the defaultreconifyMistralHandler.setSessionTimeout(15)SeeExamples with MistralIntegrate the module with PerplexityThe following instructions are for Perplexity.Import the modulefromreconifyimportreconifyUniversalHandlerInitialize the moduleConfigure the instance of Reconify passing the Reconify API_KEY and APP_KEY created above.reconifyUniversalHandler.config(appKey='Your_App_Key',apiKey='Your_Api_Key')After making a call to the Perplexity, send the input parameters and response data to Reconify, along with the start and end time stamps.input={"model":"pplx-7b-chat","messages":[{"role":"system","content":"You are a comic"},{"role":"user","content":"Tell a cat joke"},]}start=reconifyUniversalHandler.getTimestamp()response=requests.post("https://api.perplexity.ai/chat/completions",json=input,headers={"accept":"application/json","content-type":"application/json","authorization":"Bearer YOUR_PERPLEXITY_KEY"})end=reconifyUniversalHandler.getTimestamp()reconifyUniversalHandler.logChat(input,response.text,start,end)Optional Config ParametersThere are additional optional parameters that can be passed in to the handler.debug: (default False) Enable/Disable console loggingtrackImages: (default True) Turn on/off tracking of createImageFor example:reconifyUniversalHandler.config(client,appKey='Your_App_Key',apiKey='Your_Api_Key',debug=True)Optional methodsYou can optionally pass in a user object or session ID to be used in the analytics reporting.
The session ID will be used to group interactions together in the same session transcript.Set a userThe user JSON should include a unique userId, all the other fields are optional.
Without a unique userId, each user will be treated as a new user.reconifyUniversalHandler.setUser({"userId":"ABC123","firstName":"Francis","lastName":"Smith"})Set a Session IDThe Session ID is an alphanumeric string.reconifyUniversalHandler.setSession('MySessionId')Set Session TimeoutSet the session timeout in minutes to override the defaultreconifyUniversalHandler.setSessionTimeout(15)SeeExamples with PerplexityExamples with OpenAIChat ExamplefromopenaiimportOpenAIfromreconifyimportreconifyOpenAIHandleropenai_client=OpenAI(api_key='YOUR_OPENAI_KEY')reconifyOpenAIHandler.config(openai_client,'Your_App_Key','Your_Api_Key')reconifyOpenAIHandler.setUser({"userId":"12345","isAuthenticated":1,"firstName":"Jim","lastName":"Stand","gender":"male"})response=openai_client.chat.completions.create(model="gpt-3.5-turbo",messages=[{"role":"system","content":"You are an expert on commedians."},{"role":"user","content":"Tell a joke about cats"},],temperature=0,)Completion ExamplefromopenaiimportOpenAIfromreconifyimportreconifyOpenAIHandleropenai_client=OpenAI(api_key='YOUR_OPENAI_KEY')reconifyOpenAIHandler.config(openai_client,'Your_App_Key','Your_Api_Key')reconifyOpenAIHandler.setUser({"userId":"12345","isAuthenticated":1,"firstName":"Jim","lastName":"Stand","gender":"male"})response=openai_client.completions.create(model="text-davinci-003",prompt="write a haiku about cats",max_tokens=100,temperature=0,)Image ExamplefromopenaiimportOpenAIfromreconifyimportreconifyOpenAIHandleropenai_client=OpenAI(api_key='YOUR_OPENAI_KEY')reconifyOpenAIHandler.config(openai_client,'Your_App_Key','Your_Api_Key')reconifyOpenAIHandler.setUser({"userId":"12345","isAuthenticated":1,"firstName":"Jim","lastName":"Stand","gender":"male"})response=openai_client.images.generate(model="dall-e-3",prompt="a cat on the moon",n=1,size="1024x1024",quality="standard",response_format="url")Examples with Amazon Bedrock RuntimeAnthropic Claude exampleimportboto3fromreconifyimportreconifyBedrockRuntimeHandlerbedrock=boto3.client('bedrock-runtime')reconifyBedrockRuntimeHandler.config(bedrock,'Your_App_Key','Your_Api_Key')reconifyOpenAIHandler.setUser({"userId":"12345","firstName":"Jane","lastName":"Smith"})response=bedrock.invoke_model(modelId="anthropic.claude-instant-v1",contentType="application/json",accept="application/json",body="{\"prompt\":\"\\n\\nHuman: Tell a cat joke.\\n\\nAssistant:\",\"max_tokens_to_sample\":300,\"temperature\":1,\"top_k\":250,\"top_p\":0.999,\"stop_sequences\":[\"\\n\\nHuman:\"],\"anthropic_version\":\"bedrock-2023-05-31\"}")#The botocore.response.StreamingBody object will be converted to JSON and saved in parsedBodyprint(response.get("parsedBody"))Stable Diffusion image exampleimportboto3fromreconifyimportreconifyBedrockRuntimeHandlerbedrock=boto3.client('bedrock-runtime')reconifyBedrockRuntimeHandler.config(bedrock,'Your_App_Key','Your_Api_Key')reconifyOpenAIHandler.setUser({"userId":"12345","firstName":"Jane","lastName":"Smith"})response=bedrock.invoke_model(modelId="stability.stable-diffusion-xl-v0",contentType="application/json",accept="application/json",body="{\"text_prompts\":[{\"text\":\"a cat drinking boba tea\"}],\"cfg_scale\":10,\"seed\":0,\"steps\":50}")#The botocore.response.StreamingBody object will be converted to JSON and saved in parsedBody#The following will print out the image result in JSON base64print(response.get("parsedBody"))Examples with AnthropicCompletions ExamplefromanthropicimportAnthropic,HUMAN_PROMPT,AI_PROMPTfromreconifyimportreconifyAnthropicHandleranthropic=Anthropic(api_key='YOUR_ANTHROPIC_KEY')reconifyAnthropicHandler.config(anthropic,'Your_App_Key','Your_Api_Key')reconifyAnthropicHandler.setUser({"userId":"12345","firstName":"Jim","lastName":"Smith",})response=anthropic.completions.create(model="claude-2",max_tokens_to_sample=300,prompt=f"{HUMAN_PROMPT}Tell me a good cat joke{AI_PROMPT}",)Examples with CohereChat ExamplefromcohereimportClientfromreconifyimportreconifyCohereHandlercohere_client=Client('YOUR_COHERE_KEY')reconifyCohereHandler.config(cohere_client,'Your_App_Key','Your_Api_Key')reconifyCohereHandler.setUser({"userId":"12345","firstName":"Jim","lastName":"Smith"})response=cohere_client.chat(model="command",message="tell me a good cat joke")Generate ExamplefromcohereimportClientfromreconifyimportreconifyCohereHandlercohere_client=Client('YOUR_COHERE_KEY')reconifyCohereHandler.config(cohere_client,'Your_App_Key','Your_Api_Key')reconifyCohereHandler.setUser({"userId":"12345","firstName":"Jim","lastName":"Smith"})response=cohere_client.generate(model="command",prompt="write a cat haiku",max_tokens=300)Examples with MistralChat Examplefrommistralai.clientimportMistralClientfrommistralai.models.chat_completionimportChatMessagefromreconifyimportreconifyMistralHandlerclient=MistralClient(api_key='YOUR_MISTRAL_KEY')reconifyMistralHandler.config(client,'Your_App_Key','Your_Api_Key')reconifyMistralHandler.setUser({"userId":"12345","firstName":"Jim","lastName":"Smith"})response=client.chat(model="mistral-tiny",messages=[ChatMessage(role="user",content="Tell me a cat joke")])Examples with PerplexityChat ExampleimportrequestsfromreconifyimportreconifyUniversalHandlerreconifyUniversalHandler.config('Your_App_Key','Your_Api_Key')reconifyUniversalHandler.setUser({"userId":"12345","firstName":"Jim","lastName":"Smith"})input={"model":"pplx-7b-chat","messages":[{"role":"system","content":"You are a comic"},{"role":"user","content":"Tell a cat joke"},]}start=reconifyUniversalHandler.getTimestamp()response=requests.post("https://api.perplexity.ai/chat/completions",json=input,headers={"accept":"application/json","content-type":"application/json","authorization":"Bearer YOUR_PERPLEXITY_KEY"})end=reconifyUniversalHandler.getTimestamp()reconifyUniversalHandler.logChat(input,response.text,start,end)