Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(openai): a few refactors #134

Merged
merged 20 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions openai/api.w

This file was deleted.

4 changes: 2 additions & 2 deletions openai/example.main.w → openai/examples/example.main.w
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
bring expect;
bring "./openai.w" as openai;
bring "../openai.w" as openai;

bring cloud;

let oai = new openai.OpenAI(apiKey: "my-openai-key");

new cloud.Function(inflight () => {
let answer = oai.createCompletion("tell me a short joke", model: "gpt-3.5-turbo", max_tokens: 2048);
let answer = oai.createCompletion("tell me a short joke", model: "gpt-3.5-turbo", maxTokens: 2048);
log(answer);
}) as "tell me a joke";
10 changes: 3 additions & 7 deletions openai/openai.extern.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export default interface extern {
createNewInflightClient: (apiKey: string, org?: (string) | undefined) => Promise<IOpenAI$Inflight>,
createNewInflightClient: (apiKey: string, org?: (string) | undefined) => Promise<IClient$Inflight>,
}
export interface CompletionParams {
readonly max_tokens: number;
readonly model: string;
}
export interface IOpenAI$Inflight {
readonly createCompletion: (prompt: string, params?: (CompletionParams) | undefined) => Promise<string>;
export interface IClient$Inflight {
readonly createCompletion: (params: Readonly<any>) => Promise<Readonly<any>>;
}
25 changes: 2 additions & 23 deletions openai/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,9 @@ exports.createNewInflightClient = (apiKey, org) => {

let client = new openai.OpenAI(config);

// TODO: this is a hack for now, we should model the openai api in the api.w file with more fidelity
// and then we can just return the client itself, like we do in redis
return {
createCompletion: async (prompt, params = {}) => {
if (!prompt) {
throw new Error("Prompt is required");
};

if (typeof prompt !== "string") {
throw new Error("Prompt must be a string");
}

if (!params.model) {
params.model = "gpt-3.5-turbo";
};

if (!params.max_tokens) {
params.max_tokens = 2048;
}

params.messages = [ { role: 'user', content: prompt } ];

const response = await client.chat.completions.create(params);
return response.choices[0]?.message?.content;
createCompletion: async params => {
return await client.chat.completions.create(params);
}
};
};
10 changes: 5 additions & 5 deletions openai/openai.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ bring "./openai.w" as openai;

bring cloud;

let key = "my-openai-key";
let oai = new openai.OpenAI(apiKey: key);
let oai = new openai.OpenAI(apiKey: "dummy-key");

test "basic completion" {
let answer = oai.createCompletion("tell me a short joke", model :"gpt-3.5-turbo", max_tokens: 1024);
let answer = oai.createCompletion("tell me a short joke", maxTokens: 1024);

// in tests, the response is just an echo of the request
expect.equal(answer, Json.stringify({
mock: {
prompt:"tell me a short joke",
params:{"model":"gpt-3.5-turbo","max_tokens":1024}
"max_tokens":1024,
"model":"gpt-3.5-turbo",
"messages":[{"role":"user","content":"tell me a short joke"}]
}
}));
}
45 changes: 34 additions & 11 deletions openai/openai.w
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
bring util;
bring "./api.w" as api;
bring "./utils.w" as utils;
bring cloud;
bring util;

pub struct CompletionParams {
model: str?;
maxTokens: num?;
}

pub struct OpenAIProps {
apiKey: str?;
Expand All @@ -10,21 +13,33 @@ pub struct OpenAIProps {
orgSecret: cloud.Secret?;
}

inflight class Sim impl api.IOpenAI {
pub createCompletion(prompt: str, params: api.CompletionParams?): str {
return Json.stringify({ mock: { prompt: prompt, params: params } });
interface IClient {
inflight createCompletion(params: Json): Json;
}

inflight class Sim impl IClient {
pub createCompletion(req: Json): Json {
return {
choices: [
{
message: {
content: Json.stringify({ mock: req })
}
}
]
};
}
}

pub class OpenAI impl api.IOpenAI {
pub class OpenAI {
apiKey: cloud.Secret?;
org: cloud.Secret?;
keyOverride: str?;
orgOverride: str?;

mock: bool;

inflight openai: api.IOpenAI;
inflight openai: IClient;

new(props: OpenAIProps?) {
this.apiKey = props?.apiKeySecret;
Expand All @@ -50,11 +65,19 @@ pub class OpenAI impl api.IOpenAI {
if this.mock {
this.openai = new Sim();
} else {
this.openai = utils.createNewInflightClient(apiKey, org);
this.openai = OpenAI.createNewInflightClient(apiKey, org);
}
}

pub inflight createCompletion(prompt: str, params: api.CompletionParams?): str {
return this.openai.createCompletion(prompt, params);
pub inflight createCompletion(prompt: str, params: CompletionParams?): str {
let resp = this.openai.createCompletion({
max_tokens: params?.maxTokens ?? 2048,
model: params?.model ?? "gpt-3.5-turbo",
messages: [ { role: "user", content: prompt } ]
});

return resp.get("choices").getAt(0).get("message").get("content").asStr();
}

extern "./openai.js" pub static inflight createNewInflightClient(apiKey: str, org: str?): IClient;
}
2 changes: 1 addition & 1 deletion openai/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@winglibs/openai",
"description": "OpenAI library for Wing",
"version": "0.0.1",
"version": "0.0.2",
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
Expand Down
5 changes: 0 additions & 5 deletions openai/utils.w

This file was deleted.

Loading