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

proxy openai requests from js library #4 #10

Merged
merged 7 commits into from
Oct 12, 2023

Conversation

abubakarsohail
Copy link
Collaborator

@abubakarsohail abubakarsohail commented Oct 6, 2023

fix #4
This PR adds proxy support for openai node package. Also adds some unit tests.

@abubakarsohail abubakarsohail marked this pull request as draft October 6, 2023 13:38
const response = Reflect.apply(target, thisArg, argArray);
if (response instanceof Promise) {
response.then((request_response) => {
const request_end_time = new Date().toISOString();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't we need to do something special for this in JS?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we fixing that on the backend? So many users had that issue

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What issue are we talking about exactly?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People have to do this currently when using Javascript
CleanShot 2023-10-06 at 19 06 36

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jzone3 this should not have any issues according to the backend.

const newTarget = Reflect.construct(target, args);
Object.defineProperties(newTarget, {
function_name: {
value: args[1] || "openai",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the || "openai"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jzone3 still need to figure out this as well. This is likely to change given how I figure out lazy loading to work. For now consider this is just there to represent a default value. In future I expect it to be passed as a function parameter that assigned default function name just like how we do it in python.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, makes sense. We can even be more explicit "unknown_js_function" if I understand correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jzone3 totally, yes


const api_key = process.env.PROMPTLAYER_API_KEY!;

const handler: ProxyHandler<typeof BaseOpenAI> = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be openAIHandler? What happens when we add Anthropic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to figure out how we are going to do lazy loading that works across the board, if we end up with this syntax, then we will most likely create a function with generic implementation which we should be able to use with anthropic as well.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great

@abubakarsohail abubakarsohail marked this pull request as ready for review October 10, 2023 13:56
Copy link

@jzone3 jzone3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is half this PR tests? When was this discussed? I don't see it in the proposal

Please move to a separate repo as this will become public

Comment on lines 1 to 12
import { rest } from "msw";

export const handlers = [
rest.post("https://api.promptlayer.com/track-request", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
request_id: 1234567890,
})
);
}),
];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? Why are we committing it?

Comment on lines 1 to 14
import { handlers } from "@/mocks/handlers";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll } from "vitest";

export const server = setupServer(...handlers);

// Start server before all tests
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));

// Close server after all tests
afterAll(() => server.close());

// Reset handlers after each test `important for test isolation`
afterEach(() => server.resetHandlers());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? We shouldn't have tests committed

return new Proxy(newTarget, handler);
},
get: (target, prop, receiver) => {
const value = target[prop as keyof typeof target];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target[prop as keyof typeof target]

This can't be normal

@@ -1,3 +1,8 @@
import { describe, expect, it } from "vitest";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repo is going to be public. We can put tests in another repo

jzone3
jzone3 previously approved these changes Oct 10, 2023
Copy link

@jzone3 jzone3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Can you merge then add a code snippet in the README

@abubakarsohail abubakarsohail merged commit b32331c into master Oct 12, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proxy openai requests from js library
2 participants