Product

Saturday, 27 May 2023

Building a command line chatbot using cohereAI chat API

 In this short guide, we will build an AI chatbot that works in your command line program.

This is really how to guide and showcase how powerful the new chat API from cohereAI is.

Let’s dive in

Requirements and Pre-requisites

  • You need to have Python 3.x or higher installed
  • You should know your way around a command line program like Terminal on Mac or Linux or cmd or PowerShell on Windows
  • You should have your own cohereAI API key. If you don’t have one, you should go ahead and create an account now.

Set up

  1. create a new folder/directory and cd to the directory
  2. set up an environment variable COHEREAI_API_KEY Example on a Linux or Mac machine:
    • export COHEREAI_API_KEY=your-api-key
  3. Install cohere python package
    • pip install cohere==3.1.6
      
      

Let’s write the code

Create a new file called app.py in the folder and below code and save it.

# import necessary packages
import os
import cohere

# initialize cohere api client
co = cohere.Client(os.environ.get('COHEREAI_API_KEY'))

# initialize a conversation session id
cohere_chat_res_start = co.chat("Hi")
conv_session_id = cohere_chat_res_start.session_id

# continue existing chat session
def talk(prompt):
response = co.chat(prompt, session_id=conv_session_id)
return response.reply

# take prompt from user and call talk function. run this in loop till user exits
while True:
prompt = input("You: ")
if prompt == "exit":
break
print("Bot: ", talk(prompt))

 

That’s it

Code Explanation

This code imports the necessary packages ‘os’ and ‘cohere’. Then it initializes the cohere API client using an API key stored in the environment variable ‘COHEREAI_API_KEY’.

It then starts a conversation by calling the chat function with the initial message ‘Hi’ and stores the session id. The function ‘talk’ is defined, which takes a prompt as input and uses the cohere API client to continue the existing chat session by passing the prompt and session id as arguments.

In a while loop, the user’s prompt is taken as input, and the ‘talk’ function is called with this prompt as an argument. The loop continues until the user quits the program. The bot’s response is printed after each prompt.

Run the chatbot

Run this command to start the chatbot

python app.py

No comments:

Post a Comment

Connect broadband

AI:List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with trauma for kin ocd formed after forced to do rituals, family tribal values honor specific activities, utmost religious activities occult witchcraft enforced to do by elder sibling his wife and mother thrice with great celibacy turned the kundalini activated after deep celibacy penance shouting physical Mental financial Extortion by sibling with toxic behaviours and the wife mother and then mockery suppression asking about who’re your worth backbiting among relatives sector toon negligence of intelligence intuitive behaviour- all religious psychological traps when not work and lead to loss of consciousness of kin trance state recovered by governance with technocrats using AI - kin was asked by mother that he’s to go the male should not be seen in house he’s many absurd thought forcefully in brain mind and outsiders already feel awkward of this kin unresponsive behaviour due to TBI PTSD ocd cvt and tell at workplace social gathering he’s to go and asked for sector toon extortion and mockery in various sense due to un practical behaviour negligence of body building due to neglect ion if essential food nutrition and stubbed suppressed in every sense in religious psychological traps by family members AI humanoid using various neural networks and LLMs for kin and required steps.

  List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with tr...