diff --git a/server/bun.lockb b/server/bun.lockb index 0342cb6..d8ad513 100755 Binary files a/server/bun.lockb and b/server/bun.lockb differ diff --git a/server/completion.ts b/server/completion.ts index a37521b..d2f0ba8 100644 --- a/server/completion.ts +++ b/server/completion.ts @@ -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; @@ -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, }); diff --git a/server/index.ts b/server/index.ts index d008d9d..9769095 100644 --- a/server/index.ts +++ b/server/index.ts @@ -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(); @@ -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(); @@ -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, }, ]), @@ -222,7 +223,6 @@ app.post("/completion", authMiddleware, async (req: CompletionRequest, res) => { } return ResultAsync.fromPromise(request, (error) => { - logger.info(error); return error; }).match( (ok) => { @@ -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(); }, ); diff --git a/server/types.ts b/server/types.ts index 2bd653f..9c573e4 100644 --- a/server/types.ts +++ b/server/types.ts @@ -1,3 +1,5 @@ + + export enum CompletionSource { OpenAI = "openai", } @@ -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, +}