Hello World! Meet Language AI
Here we take a quick tour of what’s possible with language AI via Cohere’s Large Language Model (LLM) API. This is the Hello, World! of language AI, written for developers with little or no background in AI. In fact, we’ll do that by exploring the Hello, World! phrase itself.
Read the accompanying blog post here.
We’ll cover three groups of tasks that you will typically work on when dealing with language data, including:
- Generating text
- Classifying text
- Analyzing text
The first step is to install the Cohere Python SDK. Next, create an API key, which you can generate from the Cohere dashboard or CLI tool.
The Cohere Generate endpoint generates text given an input, called “prompt”. The prompt provides a context of what we want the model to generate text. To illustrate this, let’s start with a simple prompt as the input.
Try a Simple Prompt
Create a Better Prompt
The output is not bad, but it can be better. We need to find a way to make the output tighter to how we want it to be, which is where we leverage prompt engineering.
Automating the Process
In real applications, you will likely need to produce these text generations on an ongoing basis, given different inputs. Let’s simulate that with our example.
Cohere’s Classify endpoint makes it easy to take a list of texts and predict their categories, or classes. A typical machine learning model requires many training examples to perform text classification, but with the Classify endpoint, you can get started with as few as 5 examples per class.
Sentiment Analysis
Cohere’s Embed endpoint takes a piece of text and turns it into a vector embedding. Embeddings represent text in the form of numbers that capture its meaning and context. What it means is that it gives you the ability to turn unstructured text data into a structured form. It opens up ways to analyze and extract insights from them.
Get embeddings
Here we have a list of 50 top web search keywords about Hello, World! taken from a keyword tool. Let’s look at a few examples:
search_term | |
---|---|
0 | how to print hello world in python |
1 | what is hello world |
2 | how do you write hello world in an alert box |
3 | how to print hello world in java |
4 | how to write hello world in eclipse |
We use the Embed endpoint to get the embeddings for each of these keywords.
Semantic Search
We’ll look at a couple of example applications. The first example is semantic search. Given a new query, our “search engine” must return the most similar FAQs, where the FAQs are the 50 search terms we uploaded earlier.
We use cosine similarity to compare the similarity of the new query with each of the FAQs
Finally, we display the top 5 FAQs that match the new query
Semantic Exploration
In the second example, we take the same idea as semantic search and take a broader look, which is exploring huge volumes of text and analyzing their semantic relationships.
We’ll use the same 50 top web search terms about Hello, World! There are different techniques we can use to compress the embeddings down to just 2 dimensions while retaining as much information as possible. We’ll use a technique called UMAP. And once we can get it down to 2 dimensions, we can plot these embeddings on a 2D chart.