Skip to content

Commit d38d80b

Browse files
committed
Implement caching when calling OpenAI
1 parent 46714dc commit d38d80b

File tree

6 files changed

+8
-28
lines changed

6 files changed

+8
-28
lines changed

src/stack/callOpenAI/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import OpenAI from "openai";
1+
import { openai } from '../integrations/openai/construct';
22

33
interface OutputType {
44
methodName: string;
@@ -10,15 +10,11 @@ interface InputType {
1010
* "Call OpenAI to get the function name of the stackwise, given its skeleton"
1111
*/
1212
export async function callOpenAI({ prompt }: InputType): Promise<OutputType> {
13-
const openai = new OpenAI({
14-
apiKey: process.env.OPENAI_API_KEY,
15-
});
16-
1713
try {
1814
// Create a chat completion request
1915
const chatCompletion = await openai.chat.completions.create({
20-
messages: [{ role: "user", content: prompt }],
21-
model: "gpt-3.5-turbo",
16+
messages: [{ role: 'user', content: prompt }],
17+
model: 'gpt-3.5-turbo',
2218
});
2319

2420
// console.log('Response from OpenAI:', {
@@ -29,6 +25,6 @@ export async function callOpenAI({ prompt }: InputType): Promise<OutputType> {
2925
methodName: chatCompletion.choices[0].message.content,
3026
};
3127
} catch (error) {
32-
console.error("Error during OpenAI API request:", error);
28+
console.error('Error during OpenAI API request:', error);
3329
}
3430
}

src/stack/integrations/generic/chooseBoilerplate.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import path from 'path';
2-
import OpenAI from 'openai';
32
import { Pinecone } from '@pinecone-database/pinecone';
43
import { getEmbedding } from '../../../../stacks/openai/getEmbedding';
54
import { readExplainFiles } from '../lib/utils';
65
import { BoilerplateMetadata } from '../lib/types';
76
import { combineSkeleton } from '../../createSkeleton';
7+
import { openai } from '../openai/construct';
88

99
const pinecone = new Pinecone({
1010
apiKey: process.env.PINECONE_API_KEY as string,
1111
environment: process.env.PINECONE_ENVIRONMENT as string,
1212
});
1313

14-
const openai = new OpenAI({
15-
apiKey: process.env.OPENAI_API_KEY,
16-
});
17-
1814
interface OutputType {
1915
nearestBoilerplate: BoilerplateMetadata | BoilerplateMetadata[] | null;
2016
integration: string;

src/stack/integrations/openai/boilerplate.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import OpenAI from 'openai';
2-
3-
const openai = new OpenAI({
4-
apiKey: process.env.OPENAI_API_KEY as string,
5-
});
1+
import { openai } from './construct';
62

73
export async function openAIQueryBoilerplate(prompt: string): Promise<string> {
84
// this function generates some text given a prompt and return the exact text

src/stack/integrations/pinecone/construct/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ const pinecone = new Pinecone({
77
environment: process.env.PINECONE_ENVIRONMENT as string,
88
});
99

10-
const openai = new OpenAI({
11-
apiKey: process.env.OPENAI_API_KEY as string,
12-
});
13-
1410
export default async function constructPinecone(
1511
briefSkeleton: string,
1612
functionAndOutputSkeleton: string,

src/stack/integrations/replicate/construct/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import OpenAI from 'openai';
21
import { Pinecone } from '@pinecone-database/pinecone';
32
import { BoilerplateMetadata } from '../../lib/types';
43
import { boilerplateExpert, furtherEngineering, userAsk } from './prompts';
5-
6-
const openai = new OpenAI({
7-
apiKey: process.env.OPENAI_APY_KEY,
8-
});
4+
import { openai } from '../../openai/construct';
95

106
const pinecone = new Pinecone({
117
apiKey: process.env.PINECONE_API_KEY as string,

test-workspace/fixtures/inputEmptyString/output.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ stack('this is an example', {
55

66
import generateBooleanResponse from '../../stacks/generateBooleanResponse';
77

8-
await generateBooleanResponse("''");
8+
await generateBooleanResponse('');
99

1010

1111
/**

0 commit comments

Comments
 (0)