Skip to content

Commit 076c7e3

Browse files
committed
v0.1.0 done and done
1 parent b1da42f commit 076c7e3

File tree

6 files changed

+319
-2
lines changed

6 files changed

+319
-2
lines changed

README.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,98 @@
1-
# Cohere_Client_Arduino
2-
https://github.com/ejri/Cohere_Client
1+
[![arduino-library-badge](https://www.ardu-badge.com/badge/Cohere_Client_Arduino.svg?)](https://www.ardu-badge.com/Cohere_Client_Arduino)
2+
3+
4+
# Cohere Client For Arduino
5+
6+
This library is to connect Cohere models to the Arduino environment. It is a work in progress, with the bare metal features currently, but should be sufficient for IoT and other types of integrations.
7+
8+
This library requires users to have their own Cohere API key. Users must obtain an API Key from Cohere to use it.
9+
10+
## Authentication
11+
12+
The Cohere API uses API keys for authentication. Visit your API Keys page to retrieve the API key you'll use in your requests.
13+
14+
First, get your SECRET KEY issued here.
15+
https://dashboard.cohere.ai/welcome/register
16+
![API Keys](misc/cohere_register.png)
17+
18+
## Installation
19+
20+
### Using Arduino Library Manager
21+
22+
From Arduino IDE, go to menu *Sketch -> Include Library -> Manage Libraries...*
23+
In Library Manager Window, search *"Cohere"* in the search form then select *"Cohere_Client"*
24+
Click *"Install"* button.
25+
26+
### Manual installation
27+
28+
Download zip file from this repository by selecting the green *"Code"* dropdown at the top of repository, select *"Download ZIP"*
29+
From Arduino IDE, select menu *Sketch -> Include Library -> Add .ZIP Library...*
30+
31+
Or use git:
32+
```
33+
cd ~/arduino/libraries/
34+
gh repo clone ejri/Cohere_Client_Arduino
35+
```
36+
37+
Then you should see the examples and be able to include the library in your projects with:
38+
39+
```
40+
#include <cohereclient.hpp>
41+
```
42+
43+
## Usage
44+
45+
Example:
46+
```
47+
int max_tokens = 200;
48+
String prompt = "Once upon a time in a magical land called";
49+
String response = cohereClient.makeAPICall(max_tokens, prompt);
50+
String output = cohereClient.text_output(response);
51+
Serial.println(output);
52+
delay(5000);
53+
54+
Serial.println("");
55+
56+
String fullResponse = cohereClient.full_response(response);
57+
Serial.println(fullResponse);
58+
delay(5000);
59+
```
60+
61+
Result
62+
```
63+
for the following prompt: Once upon a time in a magical land called
64+
Generated output:
65+
\nOnce upon a time in a magical land called Acme, there lived a young boy named Timmy. Timmy was an orphan and lived with his foster parents, Mr. and Mrs. Thompson. Timmy was a kind and gentle boy, and he loved his parents very much.\n\nOne day, while walking through the woods, Timmy stumbled upon a hidden cave. Intrigued, he explored the cave and discovered a secret treasure hidden deep within. Timmy was amazed at his discovery and quickly ran home to tell his parents about it.\n\nTimmy's parents were overjoyed at his discovery and thanked him for his hard work. They told him that the treasure would be his someday, and they began to plan how they would use it to make their lives better.\n\nOver time, Timmy grew up and had a family of his own. He never forgot about the treasure he had discovered as a child, and he always planned to use it to make his
66+
67+
Full Response:
68+
HTTP/1.1 200 OK
69+
content-type: application/json
70+
num_chars: 896
71+
vary: Origin
72+
x-endpoint-monthly-call-limit: 5000
73+
x-ratelimit-limit: 10000000
74+
x-ratelimit-remaining: 9999998
75+
x-ratelimit-reset: 1684403260
76+
x-trial-endpoint-call-limit: 5
77+
x-trial-endpoint-call-remaining: 4
78+
date: Sun, 21 May 2023 00:08:58 GMT
79+
content-length: 1071
80+
Via: 1.1 google
81+
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
82+
Connection: close
83+
84+
{"id":"876cfa43-6b69-4b3a-8629-8c9c3214d818","generations":[{"id":"66809806-45b8-4322-b801-f70a65a2d842","text":"\nOnce upon a time in a magical land called Acme, there lived a young boy named Timmy. Timmy was an orphan and lived with his foster parents, Mr. and Mrs. Thompson. Timmy was a kind and gentle boy, and he loved his parents very much.\n\nOne day, while walking through the woods, Timmy stumbled upon a hidden cave. Intrigued, he explored the cave and discovered a secret treasure hidden deep within. Timmy was amazed at his discovery and quickly ran home to tell his parents about it.\n\nTimmy's parents were overjoyed at his discovery and thanked him for his hard work. They told him that the treasure would be his someday, and they began to plan how they would use it to make their lives better.\n\nOver time, Timmy grew up and had a family of his own. He never forgot about the treasure he had discovered as a child, and he always planned to use it to make his"}],"prompt":"Once upon a time in a magical land called","meta":{"api_version":{"version":"1"}}}
85+
86+
```
87+
88+
# Updates
89+
- v0.1.0
90+
- Generation model support
91+
92+
# To do list
93+
94+
- add support to other models
95+
96+
# License
97+
98+
This software is written by Ibrahim El-chami and is licensed under The MIT License. Check License file for more information.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Project Cohere Client For ESP32
3+
* Description: For HTTPS connection using WiFiClientSecure
4+
* Author: Ibrahim Elchami
5+
* Repo: https://github.com/ejri/Cohere_Client_Arduino
6+
* Date: May 20, 2023
7+
*/
8+
9+
#include <WiFiClientSecure.h>
10+
#include "cohereclient.hpp"
11+
12+
#define WIFI_SSID "WIFI_SSID"
13+
#define WIFI_PW "WIFI_PW"
14+
#define YOUR_API_KEY "YOUR_API_KEY"
15+
16+
WiFiClientSecure client;
17+
CohereClient cohereClient(&client, YOUR_API_KEY);
18+
int max_tokens = 200;
19+
String prompt = "Once upon a time in a magical land called";
20+
21+
void setup()
22+
{
23+
Serial.begin(115200);
24+
WiFi.begin(WIFI_SSID, WIFI_PW);
25+
26+
while (WiFi.status() != WL_CONNECTED)
27+
{
28+
delay(500);
29+
Serial.println("Connecting to WiFi...");
30+
}
31+
32+
Serial.println("Connected to WiFi");
33+
}
34+
35+
void loop()
36+
{
37+
String response = cohereClient.makeAPICall(max_tokens, prompt);
38+
String output = cohereClient.text_output(response);
39+
Serial.println(output);
40+
delay(5000);
41+
42+
Serial.println("");
43+
44+
String fullResponse = cohereClient.full_response(response);
45+
Serial.println(fullResponse);
46+
delay(5000);
47+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#ifndef COHERECLIENT_H
2+
#define COHERECLIENT_H
3+
4+
#include <WiFiClientSecure.h>
5+
6+
class CohereClient {
7+
public:
8+
CohereClient(WiFiClientSecure* client, const String& apiKey) : client(client), apiKey(apiKey) {}
9+
10+
String makeAPICall(int maxTokens, const String& prompt);
11+
String text_output(const String& response);
12+
String full_response(const String& response);
13+
14+
private:
15+
WiFiClientSecure* client;
16+
String apiKey;
17+
const char* host = "api.cohere.ai";
18+
const int httpsPort = 443;
19+
};
20+
21+
String CohereClient::makeAPICall(int maxTokens, const String& prompt) {
22+
client->setInsecure();
23+
24+
if (!client->connect(host, httpsPort)) {
25+
Serial.println("Connection failed!");
26+
return "";
27+
}
28+
29+
String post_body = "{\"max_tokens\":" + String(maxTokens) + ", \"prompt\":\"" + prompt + "\"}";
30+
31+
String http_request = "POST /v1/generate HTTP/1.1\r\n";
32+
http_request += "Host: " + String(host) + "\r\n";
33+
http_request += "Content-Type: application/json\r\n";
34+
http_request += "Content-Length: " + String(post_body.length()) + "\r\n";
35+
http_request += "Authorization: Bearer " + apiKey + "\r\n";
36+
http_request += "Connection: close\r\n";
37+
http_request += "\r\n";
38+
http_request += post_body + "\r\n";
39+
40+
client->print(http_request);
41+
42+
String response;
43+
while (client->connected()) {
44+
if (client->available()) {
45+
response += client->readStringUntil('\n');
46+
}
47+
}
48+
49+
client->stop();
50+
51+
return response;
52+
}
53+
54+
String CohereClient::text_output(const String& response) {
55+
int start = response.indexOf("\"text\":\"");
56+
int end = response.indexOf("\"}", start);
57+
58+
if (start == -1 || end == -1) {
59+
Serial.println("Error extracting text from response");
60+
return "";
61+
}
62+
63+
String text = response.substring(start + 8, end);
64+
String prompt = response.substring(response.indexOf("\"prompt\":\"") + 10, response.indexOf("\"", response.indexOf("\"prompt\":\"") + 10));
65+
66+
return "for the following prompt: " + prompt + "\nGenerated output:\n" + text;
67+
}
68+
69+
String CohereClient::full_response(const String& response) {
70+
String formattedResponse = response;
71+
72+
// Replace line breaks with "\n" for better visibility
73+
formattedResponse.replace("\r\n", "\n");
74+
75+
return "Full Response:\n" + formattedResponse;
76+
}
77+
78+
#endif

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Cohere_Client
2+
version=0.1.0
3+
author=Ibrahim El-chami <[email protected]>
4+
maintainer=Ibrahim El-chami <[email protected]>
5+
sentence=Arduino Library to connect with Cohere models
6+
paragraph= Client to use Cohere models in Arduino related projects
7+
url=https://github.com/ejri/Cohere_Client_Arduino
8+
category=Communication
9+
architectures=*
10+
depends= None

misc/cohere_register.png

1.12 MB
Loading

src/cohereclient.hpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Project Cohere Client For ESP32
3+
* Description: For HTTPS connection using WiFiClientSecure
4+
* Author: Ibrahim Elchami
5+
* Repo: https://github.com/ejri/Cohere_Client_Arduino
6+
* Date: May 20, 2023
7+
*/
8+
9+
#ifndef COHERECLIENT_H
10+
#define COHERECLIENT_H
11+
12+
#include <WiFiClientSecure.h>
13+
14+
class CohereClient {
15+
public:
16+
CohereClient(WiFiClientSecure* client, const String& apiKey) : client(client), apiKey(apiKey) {}
17+
18+
String makeAPICall(int maxTokens, const String& prompt);
19+
String text_output(const String& response);
20+
String full_response(const String& response);
21+
22+
private:
23+
WiFiClientSecure* client;
24+
String apiKey;
25+
const char* host = "api.cohere.ai";
26+
const int httpsPort = 443;
27+
};
28+
29+
String CohereClient::makeAPICall(int maxTokens, const String& prompt) {
30+
client->setInsecure();
31+
32+
if (!client->connect(host, httpsPort)) {
33+
Serial.println("Connection failed!");
34+
return "";
35+
}
36+
37+
String post_body = "{\"max_tokens\":" + String(maxTokens) + ", \"prompt\":\"" + prompt + "\"}";
38+
39+
String http_request = "POST /v1/generate HTTP/1.1\r\n";
40+
http_request += "Host: " + String(host) + "\r\n";
41+
http_request += "Content-Type: application/json\r\n";
42+
http_request += "Content-Length: " + String(post_body.length()) + "\r\n";
43+
http_request += "Authorization: Bearer " + apiKey + "\r\n";
44+
http_request += "Connection: close\r\n";
45+
http_request += "\r\n";
46+
http_request += post_body + "\r\n";
47+
48+
client->print(http_request);
49+
50+
String response;
51+
while (client->connected()) {
52+
if (client->available()) {
53+
response += client->readStringUntil('\n');
54+
}
55+
}
56+
57+
client->stop();
58+
59+
return response;
60+
}
61+
62+
String CohereClient::text_output(const String& response) {
63+
int start = response.indexOf("\"text\":\"");
64+
int end = response.indexOf("\"}", start);
65+
66+
if (start == -1 || end == -1) {
67+
Serial.println("Error extracting text from response");
68+
return "";
69+
}
70+
71+
String text = response.substring(start + 8, end);
72+
String prompt = response.substring(response.indexOf("\"prompt\":\"") + 10, response.indexOf("\"", response.indexOf("\"prompt\":\"") + 10));
73+
74+
return "for the following prompt: " + prompt + "\nGenerated output:\n" + text;
75+
}
76+
77+
String CohereClient::full_response(const String& response) {
78+
String formattedResponse = response;
79+
80+
// Replace line breaks with "\n" for better visibility
81+
formattedResponse.replace("\r\n", "\n");
82+
83+
return "Full Response:\n" + formattedResponse;
84+
}
85+
86+
#endif

0 commit comments

Comments
 (0)