Skip to content

Commit

Permalink
FIX requests
Browse files Browse the repository at this point in the history
  • Loading branch information
synoet authored and wabscale committed Mar 19, 2024
1 parent bc129f0 commit 95bf6c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
Binary file modified server/bun.lockb
Binary file not shown.
32 changes: 26 additions & 6 deletions server/completion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get_encoding } from "tiktoken";
import axios from "axios";
import { OPENAI_API_KEY } from ".";
import { constructPrompt } from "./prompt";
import { DEFAULT_CONFIGURATION } from "./types";

const OPEN_AI_MAX_TOKENS = 200;
const PROMPT_TOKENS = 140;
Expand Down Expand Up @@ -32,18 +32,38 @@ export function constructOpenAICompletionRequest({
context = trimmedContext;
}

const fullPrompt = constructPrompt(prompt, context, fileExtension);
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
"Authorization": `Bearer ${OPENAI_API_KEY}`,
};

const messages = [
{
role: "system",
content: `1. Complete the code provided, using the code provided.
2. DO NOT RE-INCLUDE THE CODE PROVIDED IN YOUR ANSWER.
3. DO NOT EXPLAIN ANYTHING
4. ONLY return code, do not explain anything
5. Your completion should start with the character after the code provided
6. Use the following language ${fileExtension}
Here is some context to help you get started:\n ${context}`,
},
{
role: "user",
content: prompt
}
]

const body = {
prompt: fullPrompt,
model,
max_tokens: OPEN_AI_MAX_TOKENS,
messages,
model: DEFAULT_CONFIGURATION.model,
max_tokens: 512,
};


console.log(headers)
console.log(body)

return axios.post("https://api.openai.com/v1/chat/completions", body, {
headers,
});
Expand Down
16 changes: 8 additions & 8 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import express, {
import bodyParser from "body-parser";
import { createClient } from "@supabase/supabase-js";
import { Completion, User, DocumentChange } from "shared";
import { CompletionSource } from "./types";
import { CompletionSource, DEFAULT_CONFIGURATION } from "./types";
import { constructOpenAICompletionRequest } from "./completion";

const logger = pino({
level: "info",
prettifier: require("pino-pretty")(),
});

const app = express();
Expand Down Expand Up @@ -83,7 +82,7 @@ const authMiddleware = (
next: NextFunction,
) => {
const authKey = req.headers["auth-key"];
if (!authKey || authKey !== process.env.AUTH_KEY) {
if ((!authKey || authKey !== process.env.AUTH_KEY) && process.env.PROD) {
return res.status(403).send("Forbidden: Invalid AUTH key");
}
next();
Expand Down Expand Up @@ -128,8 +127,10 @@ app.post("/user", authMiddleware, async (req: UserRequest, res) => {
supabase.from("UserSettings").insert([
{
net_id: req.body.netId,
model: OpenAIModel.Turbo35,
source: CompletionSource.OpenAI,
source: DEFAULT_CONFIGURATION.source,
model: DEFAULT_CONFIGURATION.model,
url: DEFAULT_CONFIGURATION.url,
maxTokens: DEFAULT_CONFIGURATION.maxTokens,
enabled: true,
},
]),
Expand Down Expand Up @@ -222,7 +223,6 @@ app.post("/completion", authMiddleware, async (req: CompletionRequest, res) => {
}

return ResultAsync.fromPromise(request, (error) => {
logger.info(error);
return error;
}).match(
(ok) => {
Expand All @@ -234,8 +234,8 @@ app.post("/completion", authMiddleware, async (req: CompletionRequest, res) => {
.send();
},
(err) => {
logger.error(err);
req.log.error(err);
logger.error(err.message)
// req.log.error(err);
return res.status(500).send();
},
);
Expand Down
13 changes: 13 additions & 0 deletions server/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


export enum CompletionSource {
OpenAI = "openai",
}
Expand All @@ -13,5 +15,16 @@ export type OpenAISource = {
messages: string[];
};

export enum OpenAIModels {
GPT3 = "gpt-3.5-turbo-0125",
}

// NOTE: can extend this if needed
export type Source = OpenAISource;

export const DEFAULT_CONFIGURATION = {
url: "https://api.openai.com/v1/chat/completions",
source: CompletionSource.OpenAI,
model: OpenAIModels.GPT3,
maxTokens: 2000,
}

0 comments on commit 95bf6c0

Please sign in to comment.