Skip to content

Commit

Permalink
Fix non-stream openai
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Dec 12, 2024
1 parent cf5600e commit 6485e67
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/r2ai.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static char *r2ai_openai(const char *content, const char *model, char **error) {
PJ *pj = pj_new ();
pj_o (pj);
pj_ks (pj, "model", model? model: "gpt-4o-mini");
pj_kb (pj, "stream", true);
pj_kb (pj, "stream", false);
pj_kn (pj, "max_completion_tokens", 5128);
pj_ka (pj, "messages");
pj_o (pj);
Expand Down Expand Up @@ -103,7 +103,7 @@ static bool handle_openai_stream_chunk(const char *chunk) {
}
r_json_free (jres);
}
free(chunk_copy);
free (chunk_copy);
}
return false;
}
Expand Down Expand Up @@ -208,24 +208,26 @@ static char *r2ai_openapi(const char *content, char **error) {
}

static bool handle_anthropic_stream_chunk(const char *chunk) {
if (!chunk || !*chunk) {
if (R_STR_ISEMPTY (chunk)) {
return false;
}

if (r_str_startswith(chunk, "event:")) {
if (r_str_startswith (chunk, "event:")) {
const char *event = chunk + 7;
while (*event == ' ') {
event++;
}
return !strcmp(event, "message_stop");
return !strcmp (event, "message_stop");
}

if (!r_str_startswith(chunk, "data:")) {
if (!r_str_startswith (chunk, "data:")) {
return false;
}

const char *data = chunk + 6;
while (*data == ' ') data++;
while (*data == ' ') {
data++;
}
if (!*data) {
return false;
}
Expand All @@ -237,7 +239,7 @@ static bool handle_anthropic_stream_chunk(const char *chunk) {
return false;
}

const RJson *type = r_json_get(jres, "type");
const RJson *type = r_json_get (jres, "type");
if (!type || !type->str_value) {
r_json_free (jres);
free (data_copy);
Expand Down Expand Up @@ -293,15 +295,15 @@ static char *r2ai_anthropic_stream(const char *content, const char *model_name,
char *apikey = r_sys_getenv ("ANTHROPIC_API_KEY");
if (!apikey) {
char *apikey_file = r_file_new ("~/.r2ai.anthropic-key", NULL);
apikey = r_file_slurp(apikey_file, NULL);
apikey = r_file_slurp (apikey_file, NULL);
free(apikey_file);
if (!apikey) {
if (error) {
*error = strdup ("Failed to read Anthropic API key from ANTHROPIC_API_KEY env or ~/.r2ai.anthropic-key");
}
return NULL;
}
r_str_trim(apikey);
r_str_trim (apikey);
}

char *auth_header = r_str_newf ("x-api-key: %s", apikey);
Expand Down Expand Up @@ -392,7 +394,7 @@ static char *r2ai_anthropic(const char *content, const char *model_name, char **

const char *anthropic_url = "https://api.anthropic.com/v1/messages";

PJ *pj = pj_new();
PJ *pj = pj_new ();
pj_o(pj);
pj_ks(pj, "model", model_name);
pj_kn(pj, "max_tokens", 4096);
Expand Down Expand Up @@ -565,6 +567,7 @@ RCorePlugin r_core_plugin_r2ai_client = {
.name = "r2ai-client",
.desc = "remote r2ai client using http post",
.author = "pancake",
.version = "0.9.2",
.license = "MIT",
},
.init = r2ai_init,
Expand Down

0 comments on commit 6485e67

Please sign in to comment.