randydev commited on
Commit
be39d49
·
verified ·
1 Parent(s): 51e8b0c

Update lib/@randydev/together/cohere.js

Browse files
Files changed (1) hide show
  1. lib/@randydev/together/cohere.js +20 -17
lib/@randydev/together/cohere.js CHANGED
@@ -1,20 +1,23 @@
1
- import got from 'got';
2
 
3
- async function CohereAI(message) {
4
- const response = await got.post('https://api.cohere.com/v1/chat', {
5
- headers: {
6
- 'Authorization': 'Bearer ' + process.env['COHERE_API_KEY'],
7
- 'Content-Type': 'application/json'
8
- },
9
- json: {
10
- "model": "command-r-08-2024",
11
- 'message': `${message}`,
12
- "temperature": 0.3,
13
- "chat_history": [],
14
- "prompt_truncation": "AUTO",
15
- "stream": false
16
- }
17
- }).json();
18
- return response;
 
 
 
19
  }
20
  export { CohereAI };
 
1
+ import { CohereClient } from "cohere-ai";
2
 
3
+ const cohere = new CohereClient({
4
+ token: process.env["COHERE_API_KEY"],
5
+ });
6
+
7
+ function CohereAI(message) {
8
+ const stream = await cohere.chatStream({
9
+ model: "command-r-08-2024",
10
+ message: `${message}`,
11
+ temperature: 0.3,
12
+ chatHistory: [],
13
+ promptTruncation: "AUTO"
14
+ });
15
+ let answer = "";
16
+ for await (const chat of stream) {
17
+ if (chat.eventType === "text-generation") {
18
+ answer += chat.text;
19
+ }
20
+ }
21
+ return answer
22
  }
23
  export { CohereAI };