Skip to content

Commit

Permalink
Add ts jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-lippert committed Dec 15, 2023
1 parent cbb8dae commit b1a353c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
48 changes: 48 additions & 0 deletions openai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import OpenAI from 'openai'

/**
* @param {{ runId: string, openai: OpenAI, threadId: string, assistant_id: string, instructions?: string|null }} options
*/
export async function getOrCreateRun({ runId, openai, threadId, assistant_id, instructions }) {
const run = runId
? await openai.beta.threads.runs.retrieve(threadId, runId)
: await openai.beta.threads.runs.create(threadId, {
assistant_id,
instructions,
})
return run
}

/**
* @param {{ threadId: string, openai: OpenAI }} options
*/
export async function getOrCreateThread({ threadId, openai }) {
const thread = threadId === 'new' ? await openai.beta.threads.create() : await openai.beta.threads.retrieve(threadId)
return thread
}

/**
* @param {{ threadId: string, openai: OpenAI, content: string }} options
*/
export async function createMessage({ openai, threadId, content }) {
const message = await openai.beta.threads.messages.create(threadId, {
role: 'user',
content,
})
return message
}

/**
* @param {{ assistantId: string, openai: OpenAI, name?: string, instructions?: string, model: string }} options
*/
export async function getAssistant({ assistantId, openai, name, instructions, model }) {
const assistant =
assistantId === 'new'
? await openai.beta.assistants.create({
name,
instructions,
model: model || 'gpt-3.5-turbo',
})
: await openai.beta.assistants.retrieve(assistantId)
return assistant
}
31 changes: 3 additions & 28 deletions worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { API, json, requiresAuth } from 'apis.do'
import OpenAI from 'openai'
import webhooks from './github-webhooks.js'
import { getAssistant, getOrCreateThread, getOrCreateRun, createMessage } from './openai.js'

const api = new API(
{
Expand Down Expand Up @@ -45,7 +46,7 @@ api.createRoute('POST', '/assistants/:assistantId', requiresAuth, async (req, en
api.get('/threads/:threadId', requiresAuth, async (req, env) => {
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY })
const { threadId } = req.params
const thread = threadId === 'new' ? await openai.beta.threads.create() : await openai.beta.threads.retrieve(threadId)
const thread = await getOrCreateThread({ threadId, openai })
const endpoints = {
thread: new URL(`threads/${thread.id}`, 'https://gpt.do'),
messages: new URL(`threads/${thread.id}/messages`, 'https://gpt.do'),
Expand All @@ -66,13 +67,7 @@ api.get('/threads/:threadId/run/:runId?', requiresAuth, async (req, env) => {
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY })
const { threadId, runId } = req.params
const { assistant: assistant_id, instructions } = req.query
const run = runId
? await openai.beta.threads.runs.retrieve(threadId, runId)
: await openai.beta.threads.runs.create(threadId, {
assistant_id,
instructions,
})
return run
return await getOrCreateRun({ runId, openai, threadId, assistant_id, instructions })
})
api.get('/threads/:threadId/:message', requiresAuth, async (req, env) => {
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY })
Expand All @@ -93,26 +88,6 @@ api.createRoute('POST', '/post', requiresAuth, handler)
api.createRoute('POST', '/:message?', requiresAuth, handler)
api.createRoute('POST', '/:template/:templateId/:message?', requiresAuth, handler)

async function createMessage({ openai, threadId, content }) {
const message = await openai.beta.threads.messages.create(threadId, {
role: 'user',
content,
})
return message
}

async function getAssistant({ assistantId, openai, name, instructions, model }) {
const assistant =
assistantId === 'new'
? await openai.beta.assistants.create({
name,
instructions,
model,
})
: await openai.beta.assistants.retrieve(assistantId)
return assistant
}

async function handler(req, env) {
async function getCompletion(options) {
return await fetch('https://api.openai.com/v1/chat/completions', {
Expand Down

0 comments on commit b1a353c

Please sign in to comment.