-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
239 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/backend/src/routes/utils/handlers/autocomplete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { z } from "zod"; | ||
import OpenAI from "openai"; | ||
import { Context } from "#lib/context"; | ||
|
||
const inputSchema = z.object({ | ||
context: z.string().optional(), | ||
paragraph: z.string() | ||
}); | ||
const outputSchema = z.object({ | ||
autocompletion: z.string() | ||
}); | ||
const handler = async ( | ||
ctx: Context, | ||
input: z.infer<typeof inputSchema> | ||
): Promise<z.infer<typeof outputSchema>> => { | ||
const openai = new OpenAI({ | ||
apiKey: ctx.fastify.config.OPENAI_API_KEY, | ||
organization: ctx.fastify.config.OPENAI_ORGANIZATION | ||
}); | ||
const prompt = `Briefly and concisely autocomplete the sentence to fit the provided context. DO NOT INCLUDE THE SENTENCE IN THE OUTPUT.\nContext: ${input.context || ""}\nSentence: ${input.paragraph}\nAutocompletion:`; | ||
const result = await openai.chat.completions.create({ | ||
model: "gpt-3.5-turbo", | ||
temperature: 0, | ||
max_tokens: 32, | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: prompt | ||
} | ||
] | ||
}); | ||
|
||
let autocompletion = result.choices[0].message.content || ""; | ||
|
||
if (input.paragraph.endsWith(" ")) { | ||
autocompletion = autocompletion.trim(); | ||
} | ||
|
||
console.log( | ||
JSON.stringify( | ||
{ | ||
prompt: `Briefly and concisely autocomplete the sentence to fit the provided context. DO NOT INCLUDE THE SENTENCE IN THE OUTPUT.\nContext: ${input.context || ""}\nSentence: ${input.paragraph}\nAutocompletion:`, | ||
autocompletion | ||
}, | ||
null, | ||
2 | ||
) | ||
); | ||
autocompletion = autocompletion.replace(input.paragraph, ""); | ||
|
||
return { autocompletion }; | ||
}; | ||
|
||
export { inputSchema, outputSchema, handler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters