Skip to content

Commit

Permalink
Added dup and opt
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrflor committed Jan 29, 2023
1 parent fdbdc05 commit 6386a45
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# openai-c
This is a pure c implementation of a clien the openai api using libcurl
This is a pure c implementation of a client for openai gpt3 api using libcurl

## License
MIT do the Hell you want with it
Expand Down
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ code:
implement duphandle
split with proper name or foldering
figure out proper name for a good api
add option to set model max_tokens and temperature
maybe use some void thing on Openai ??
add suport for stream

Expand Down
12 changes: 11 additions & 1 deletion easy.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "easy.h"
#include <curl/curl.h>

const char *format = "{\"prompt\":\"%s\",\"max_tokens\": %d,\"stop\":\"\","
"\"model\": \"%s\",\"temperature\": %f}";
Expand All @@ -9,6 +8,7 @@ struct Openai_easy {
char *model;
char **stop;
int max_tokens;
char *template_prompt;
double temperature;
};

Expand Down Expand Up @@ -39,6 +39,16 @@ OpenAI *openai_easy_init(char *api_key) {
return openai;
}

OpenAI *openai_easy_duphandle(OpenAI *openai) {
OpenAI *openai_dup = (OpenAI *)malloc(sizeof(OpenAI));
openai_dup->curl = curl_easy_duphandle(openai->curl);
openai_dup->model = openai->model;
openai_dup->max_tokens = OPENAI_DEFAULT_MAX_TOKENS;
openai_dup->temperature = OPENAI_DEFAULT_TEMPERATURE;

return openai_dup;
}

CURLcode openai_easy_setopt(OpenAI *openai, OpenAIOption option, ...) {

va_list args;
Expand Down
2 changes: 1 addition & 1 deletion easy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
Expand All @@ -15,6 +14,7 @@ typedef enum {
OPENAI_MODEL,
OPENAI_MAX_TOKENS,
OPENAI_TEMPERATURE,
OPENAI_TEMPLATE_PROMPT,
} OpenAIOption;

typedef struct Openai_easy OpenAI;
Expand Down
6 changes: 2 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "easy.h"

char *openai_main_getinput() {
char *openai_main_getinput(void) {
char *request;
printf("You: ");
size_t size = 0;
Expand All @@ -16,9 +16,7 @@ int main(int argc, char *argv[]) {
return 1;
}

OpenAI *openai;
openai = openai_easy_init(openai_api_key);
openai_easy_setopt(openai, OPENAI_MAX_TOKENS, 12);
OpenAI *openai = openai_easy_init(openai_api_key);

while (1) {
char *request = openai_main_getinput();
Expand Down

0 comments on commit 6386a45

Please sign in to comment.