Skip to content

Instantly share code, notes, and snippets.

@sgomez
Last active September 22, 2024 10:28
Show Gist options
  • Save sgomez/7f6ee457a404fbff65af039af29bdc23 to your computer and use it in GitHub Desktop.
Save sgomez/7f6ee457a404fbff65af039af29bdc23 to your computer and use it in GitHub Desktop.
Telegram bot with Vercel SDK AI and ollama provider
import dotenv from "dotenv";
import { Telegraf } from "telegraf";
import { message } from "telegraf/filters";
import process from "node:process";
import { ollama } from "ollama-ai-provider";
import { generateText, type CoreMessage, type StreamTextResult } from "ai";
dotenv.config();
const conversations: Map<number, CoreMessage[]> = new Map();
async function main() {
const bot = new Telegraf(process.env.BOT_TOKEN ?? "");
bot.start(async (ctx) => {
const chatId = ctx.chat.id;
const messages: CoreMessage[] = [];
const welcomeMessage = "Welcome! How can I assist you today?";
messages.push({ role: "assistant", content: welcomeMessage });
// Send the start message to the user
ctx.reply(welcomeMessage);
// Update conversation in memory
conversations.set(chatId, messages);
});
bot.on(message("text"), async (ctx) => {
const chatId = ctx.chat.id;
const messages = conversations.get(chatId) ?? [];
messages.push({ role: "user", content: ctx.msg.text });
ctx.sendChatAction("typing");
const { text } = await generateText({
model: ollama("llama3.1"),
system: `You are a helpful, respectful and honest assistant.`,
messages,
});
ctx.sendMessage(text);
messages.push({ role: "assistant", content: text });
conversations.set(chatId, messages);
});
bot.launch();
// Enable graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));
process.once("SIGUSR2", () => bot.stop("SIGUSR2"));
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment