Product

Wednesday, 24 May 2023

Hands-on Guide to Using cohere AI APIs with Python

 

Requirements

You are going to need to cohere AI API Key. You can get a free account from here.

After logging in, go to Dashboard -> API Keys.

While you are developing and testing, you can use a trial API key. cohere doesn’t charge you when you use the Trial API Key. However, it is recommended to use a Production API key for an app that you will release publicly.

Setup

The first thing you need to do is install cohere’s Python package. You can do that by running the below command:

pip install cohere

Your first call to cohere AI APIs with Python

Create a new python file, a Jupyter Notebook, or a Google Colab Notebook

First, we import the cohere python package and initiate a new cohere client with our cohere API key.

import cohere

cohere_api_key = "your cohere API key"

co = cohere.Client(cohere_api_key)

Below we are defining a method for calling the cohere API’s generate endpoint. Generate endpoint takes a text prompt and outputs generated text based on that prompt.

def generate_text(user_prompt):
    response = co.generate(
        model='command-xlarge-nightly',
        prompt= user_prompt
        max_tokens=300,
        temperature=0.9,
        k=0,
        p=0.75,
        frequency_penalty=0,
        presence_penalty=0,
        stop_sequences=[],
        return_likelihoods='NONE')
    return response.generations[0].text

The main parameter to pay attention in this is the prompt. You can ignore the rest right now.

Next call this method with your own prompt. For example

cohere_api_output = geenrate_text("Write a blog post title on Space Travel in 2023")

print(cohere_api_output)

This will give you the generated text from the cohere AI APIs with Python.

Conclusion

In this guide, I covered just one endpoint for using cohere AI APIs with Python. However, there are multiple endpoints. Check for more cohere AI posts here.

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...