Skip to content

Commit

Permalink
feat: update base bot template
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Oct 13, 2024
1 parent a050931 commit 56300d8
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 20 deletions.
10 changes: 6 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
"mtxr.sqltools",
"mtxr.sqltools-driver-pg",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"editorconfig.editorconfig"
"esbenp.prettier-vscode"
],
"settings": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.fixAll.eslint": "explicit"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.formatOnSave": true,
"eslint.format.enable": true,
"sqltools.connections": [
Expand All @@ -37,7 +36,10 @@
"username": "postgres",
"password": "postgres"
}
]
],
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/ai/embeddings.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { embed, embedMany } from 'ai'
import { cosineDistance, desc, sql } from 'drizzle-orm'
import { cosineDistance, desc, gt, sql } from 'drizzle-orm'

import { registry } from '../../setup-registry'
import { db as database } from '../db'
import { embeddings } from '../db/schema/embeddings'
import { environment } from '../environment.mjs'
import { registry } from './setup-registry'

const embeddingModel = registry.textEmbeddingModel(environment.MODEL_EMBEDDING)

Expand Down Expand Up @@ -50,7 +50,7 @@ export const findRelevantContent = async (
const similarGuides = await database
.select({ name: embeddings.content, similarity })
.from(embeddings)
// .where(gt(similarity, 0.1))
.where(gt(similarity, 0.3))
.orderBy((t) => desc(t.similarity))
.limit(4)

Expand Down
39 changes: 39 additions & 0 deletions src/lib/ai/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { db as database } from '../db'
import { embeddings as embeddingsTable } from '../db/schema/embeddings'
import {
insertResourceSchema,
type NewResourceParameters,
resources,
} from '../db/schema/resources'
import { generateEmbeddings } from './embeddings'

export const createResource = async (
input: NewResourceParameters,
): Promise<string> => {
try {
const { content } = insertResourceSchema.parse(input)

const [resource] = await database
.insert(resources)
.values({ content })
.returning()

if (!resource) {
return 'Resource not found'
}

const embeddings = await generateEmbeddings(content)
await database.insert(embeddingsTable).values(
embeddings.map((embedding) => ({
resourceId: resource.id,
...embedding,
})),
)

return 'Resource successfully created and embedded.'
} catch (error) {
return error instanceof Error && error.message.length > 0
? error.message
: 'Error, please try again.'
}
}
27 changes: 27 additions & 0 deletions src/lib/ai/setup-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { openai as originalOpenAI } from '@ai-sdk/openai'
import {
experimental_createProviderRegistry as createProviderRegistry,
experimental_customProvider as customProvider,
} from 'ai'
import { ollama as originalOllama } from 'ollama-ai-provider'

const ollama = customProvider({
fallbackProvider: originalOllama,
languageModels: {
'qwen-2_5': originalOllama('qwen2.5'),
},
})

export const openai = customProvider({
fallbackProvider: originalOpenAI,
languageModels: {
'gpt-4o-mini': originalOpenAI('gpt-4o-mini', {
structuredOutputs: true,
}),
},
})

export const registry = createProviderRegistry({
ollama,
openai,
})
6 changes: 6 additions & 0 deletions src/lib/commands/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { CommandContext, Context } from 'grammy'

export async function start(context: CommandContext<Context>): Promise<void> {
const content = 'Welcome, how can I help you?'
await context.reply(content)
}
8 changes: 8 additions & 0 deletions src/lib/handlers/on-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Composer } from 'grammy'

export const onMessage = new Composer()

onMessage.on('message:text', async (context) => {
const userMessage = context.message.text
await context.reply(userMessage)
})
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { customAlphabet } from 'nanoid'

export const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789')

export function tomorrow(): Date {
return new Date(new Date().setDate(new Date().getDate() + 1))
}
19 changes: 6 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import process from 'node:process'

import dotenv from 'dotenv'
import { Bot } from 'grammy'

dotenv.config()
import { start } from './lib/commands/start'
import { environment } from './lib/environment.mjs'
import { onMessage } from './lib/handlers/on-message'

async function main(): Promise<void> {
const bot = new Bot(process.env.BOT_TOKEN ?? '')
const bot = new Bot(environment.BOT_TOKEN)

bot.command('start', async (context) => {
const content = 'Welcome, how can I help you?'
bot.command('start', start)

await context.reply(content)
})

bot.on('message:text', async (context) => {
const userMessage = context.message.text

await context.reply(userMessage)
})
bot.use(onMessage)

// Enable graceful stop
process.once('SIGINT', () => bot.stop())
Expand Down

0 comments on commit 56300d8

Please sign in to comment.