-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cohere): add cohere chat bot guide (#870)
Co-authored-by: Khalil Najjar <[email protected]> Co-authored-by: Khalil Najjar <[email protected]> Co-authored-by: Max Leiter <[email protected]>
- Loading branch information
1 parent
11048cd
commit 4afa012
Showing
7 changed files
with
298 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { CohereClient, Cohere } from 'cohere-ai'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
// IMPORTANT! Set the dynamic to force-dynamic | ||
// Prevent nextjs to cache this route | ||
export const dynamic = 'force-dynamic'; | ||
|
||
const cohere = new CohereClient({ | ||
token: process.env.COHERE_API_KEY || '', | ||
}); | ||
|
||
const toCohereRole = (role: string): Cohere.ChatMessageRole => { | ||
if (role === 'user') { | ||
return Cohere.ChatMessageRole.User; | ||
} | ||
return Cohere.ChatMessageRole.Chatbot; | ||
}; | ||
|
||
export async function POST(req: Request) { | ||
// Extract the `messages` from the body of the request | ||
const { messages } = await req.json(); | ||
const chatHistory = messages.map((message: any) => ({ | ||
message: message.content, | ||
role: toCohereRole(message.role), | ||
})); | ||
const lastMessage = chatHistory.pop(); | ||
|
||
const response = await cohere.chatStream({ | ||
message: lastMessage.message, | ||
chatHistory, | ||
}); | ||
|
||
const stream = new ReadableStream({ | ||
async start(controller) { | ||
for await (const event of response) { | ||
// Stream Events: https://docs.cohere.com/docs/streaming#stream-events | ||
if (event.eventType === 'text-generation') { | ||
controller.enqueue(event.text); | ||
} | ||
} | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
return new Response(stream); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use client'; | ||
|
||
import { useCompletion } from 'ai/react'; | ||
import { FormEventHandler, useState } from 'react'; | ||
|
||
export default function Chat() { | ||
const { | ||
completion, | ||
input, | ||
setInput, | ||
handleInputChange, | ||
handleSubmit, | ||
error, | ||
} = useCompletion(); | ||
|
||
const [prompt, setPrompt] = useState(''); | ||
|
||
const handleSend: FormEventHandler<HTMLFormElement> = async e => { | ||
handleSubmit(e); | ||
setPrompt(input); | ||
setInput(''); | ||
}; | ||
|
||
return ( | ||
<div className="p-4"> | ||
<header className="text-center"> | ||
<h1 className="text-xl">Completion Example</h1> | ||
</header> | ||
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch"> | ||
{error && ( | ||
<div className="fixed top-0 left-0 w-full p-4 text-center bg-red-500 text-white"> | ||
{error.message} | ||
</div> | ||
)} | ||
{completion && ( | ||
<ol className="space-y-2"> | ||
<li> | ||
<span className="font-medium">Prompt: </span> | ||
{prompt} | ||
</li> | ||
<li> | ||
<span className="font-medium">Cohere: </span> | ||
{completion} | ||
</li> | ||
</ol> | ||
)} | ||
<form onSubmit={handleSend}> | ||
<input | ||
className="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl" | ||
value={input} | ||
placeholder="Ask something..." | ||
onChange={handleInputChange} | ||
/> | ||
</form> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,34 @@ | ||
'use client'; | ||
|
||
import { useCompletion } from 'ai/react'; | ||
import { useChat } from 'ai/react'; | ||
|
||
export default function Chat() { | ||
const { completion, input, handleInputChange, handleSubmit, error } = | ||
useCompletion(); | ||
const { messages, input, handleInputChange, handleSubmit, data } = useChat(); | ||
console.log({ messages }); | ||
|
||
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 className="p-4"> | ||
<header className="text-center"> | ||
<h1 className="text-xl">Chat Example</h1> | ||
</header> | ||
<div className="flex flex-col justify-between w-full max-w-md mx-auto stretch"> | ||
<div className="flex-grow overflow-y-auto"> | ||
{messages.map(m => ( | ||
<div key={m.id} className="whitespace-pre-wrap"> | ||
{m.role === 'user' ? 'User: ' : 'AI: '} | ||
{m.content} | ||
</div> | ||
))} | ||
</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> | ||
<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> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.