Cohere
Vercel AI SDK provides a set of utilities to make it easy to use Cohere's API. In this guide, we'll walk through how to use the utilities to create a text completion app.
Guide: Text Completion
Create a Next.js app
Create a Next.js application and install ai
:
pnpm dlx create-next-app my-ai-app
cd my-ai-app
pnpm install ai
Add your Cohere API Key to .env
COHERE_API_KEY=xxxxxxx
Create a Route Handler
Create a Next.js Route Handler that uses the Edge Runtime to generate a response to a series of messages via Cohere's API, and returns the response as a streaming text response.
For this example, we'll create a route handler at app/api/completion/route.ts
that accepts a POST
request with a prompt
string:
import { StreamingTextResponse, CohereStream } from 'ai';
export async function POST(req: Request) {
// Extract the `prompt` from the body of the request
const { prompt } = await req.json();
const body = JSON.stringify({
prompt,
model: 'command-nightly',
max_tokens: 300,
stop_sequences: [],
temperature: 0.9,
return_likelihoods: 'NONE',
stream: true,
});
const response = await fetch('https://api.cohere.ai/v1/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.COHERE_API_KEY}`,
},
body,
});
// Check for errors
if (!response.ok) {
return new Response(await response.text(), {
status: response.status,
});
}
// Extract the text response from the Cohere stream
const stream = CohereStream(response);
// Respond with the stream
return new StreamingTextResponse(stream);
}
Wire up the UI
Create a Client component with a form that we'll use to gather the prompt from the user and then stream back the completion from.
By default, the useCompletion
hook will use the POST
Route Handler we created above (it defaults to /api/completion
). You can override this by passing a api
prop to useCompletion({ api: '...'})
.
'use client';
import { useCompletion } from 'ai/react';
export default function Chat() {
const { completion, input, handleInputChange, handleSubmit, error } =
useCompletion();
return (
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
<h4 className="text-xl font-bold text-gray-900 md:text-xl pb-4">
useCompletion Example
</h4>
{error && (
<div className="fixed top-0 left-0 w-full p-4 text-center bg-red-500 text-white">
{error.message}
</div>
)}
{completion}
<form onSubmit={handleSubmit}>
<input
className="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl"
value={input}
placeholder="Say something..."
onChange={handleInputChange}
/>
</form>
</div>
);
}