Skip to content

Commit

Permalink
Merge pull request #22 from crnvl/1-implement-openai-api-connection
Browse files Browse the repository at this point in the history
feat: raw openai request
  • Loading branch information
crnvl authored Nov 21, 2024
2 parents 40c3cbf + 0339972 commit 9e859c1
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=
12 changes: 11 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": ["next/core-web-vitals", "next/typescript"]
"extends": ["next/core-web-vitals", "next/typescript"],
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ yarn-debug.log*
yarn-error.log*

# env files (can opt-in for committing if needed)
.env*
.env

# vercel
.vercel
Expand Down
20 changes: 20 additions & 0 deletions app/api/openai/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { OpenAiRawRequestParams } from "@/misc/apiRoutes";
import { NextApiRequest, NextApiResponse } from "next";
import OpenAI from "openai";
const openai = new OpenAI();

const _handler = async (req: NextApiRequest, res: NextApiResponse) => {
const body: OpenAiRawRequestParams = req.body;

const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: body.prompt,
}
]
});

res.status(200).json({ data: completion.choices[0].message });
};
15 changes: 15 additions & 0 deletions misc/apiRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from 'axios';

const api = axios.create();

export const endpoints = {
openAi: {
getRawResponse: async (params: OpenAiRawRequestParams) => {
return api.post('/openai/raw', params);
},
}
};

export interface OpenAiRawRequestParams {
prompt: string;
}
Loading

0 comments on commit 9e859c1

Please sign in to comment.