PHP Classes

Ollama Artificial Intelligence PHP Telegram Bot: Telegram bot to interact with Ollama AI models

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-02 (9 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 39 All time: 10,912 This week: 47Up
Version License PHP version Categories
ollama-bot 1.0MIT/X Consortium ...7Web services, Chat, Artificial intell..., P...
Description 

Author

This package implements a Telegram bot that can interact with Ollama AI models.

It implements new commands for a Telegram bot that can use the Ollama artificial intelligence models to interact with users.

This package can use the Ollama API to send user queries entered in Telegram and return the Ollama API query responses.




Telegram bot to interact with llama models

ImplemeTelegram bot to interact with ollama models

Innovation Award
PHP Programming Innovation award winner
January 2024
Winner
The evolution of artificial intelligence applications made it possible for lay users to interact with applications like ChatGPT, for instance, which can interpret the user's natural language and respond to their questions.

On the other hand, popular chat applications like Telegram made it simple for any user to interact with other users.

This package combined the possibilities of natural language interpretation with the simplicity of Telegram to implement a chatbot application that can take user questions and provide answers using the artificial intelligence models supplied by the Ollama API.

Manuel Lemos
Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 23 packages by
Country: Peru Peru
Age: ???
All time rank: 20474 in Peru Peru
Week rank: 20 Up1 in Peru Peru Up
Innovation award
Innovation award
Nominee: 10x

Winner: 3x

Example

<?php

use Mateodioev\OllamaBot\Cache\UserCache;
use
Mateodioev\OllamaBot\Db\MysqlDatabase;
use
Mateodioev\OllamaBot\Events\{CancellCompletation, Chat, PrivateTextListener, SetModel, Start, ViewCompletionDetails};
use
Mateodioev\OllamaBot\Repository\MysqlUserRepository;
use
Mateodioev\TgHandler\{Bot, Log};

use function
Mateodioev\OllamaBot\env;

require
__DIR__ . '/vendor/autoload.php';

\
Dotenv\Dotenv::createImmutable(__DIR__)->load();

// Create logger
$logStreams = new Log\BulkStream(
    new
Log\TerminalStream(),
   
// (new Log\PhpNativeStream())->activate(env('LOG_DIR', __DIR__))
);
$logger = new Log\Logger($logStreams);

// Create bot
$bot = new Bot(env('BOT_TOKEN'), $logger);
$logStreams->add(new Log\BotApiStream($bot->getApi(), (int) env('BOT_LOG_CHANNEL_ID')));

$bot->onEvent(Start::get())
    ->
onEvent(Chat::get())
    ->
onEvent(SetModel::get())
    ->
onEvent(CancellCompletation::get())
    ->
onEvent(new PrivateTextListener())
    ->
onEvent(new ViewCompletionDetails());

$db = new MysqlDatabase('mysql:host=' . env('DB_HOST') . ';dbname=' . env('DB_NAME') . ';charset=utf8mb4', env('DB_USER'), env('DB_PASS'));
UserCache::setRepo(new MysqlUserRepository($db));

$bot->longPolling(
    (int)
env('BOT_POLLING_TIMEOUT', 60),
    (bool)
env('BOT_POLLING_IGNORE_OLD_UPDATES', false),
    (bool)
env('BOT_ASYNC', true)
);


Details

Ollama telegram bot

This bot helps you to interact with ollama models

https://github.com/Mateodioev/ollama-bot/assets/68271130/6cbec742-994b-4f79-85cf-804ec95663ad

Commands

If you want to generate a completion just send a private message to the bot or if you want to use in a public group use the command /chat followed by the text of your choice. Other commands:

  • `/setmodel` Change your current model
  • `/start`

Installation

Requirements:

  • \>= PHP 8.2
  • Mysql
  • Access to ollama api

Steps

  1. Clone this repository
    git clone https://github.com/Mateodioev/ollama-bot.git
    cd ollama-bot
    
  2. Install dependencies
    composer install --optimize-autoloader --no-interaction --no-dev
    
  3. Setup your database mysql with file `db/main.sql`
  4. Create an edit .env file
    cp .env.example .env
    vim .env # Or use your favorite editor
    
  5. Run the bot
    php index.php
    

Note > If your default model is diferent to codellama for example llama2, edit the file src/Events/Middlewares.php line 13

See ollama docs in https://github.com/jmorganca/ollama


  Files folder image Files (39)  
File Role Description
Files folder imagedb (1 file)
Files folder imagesrc (1 file, 7 directories)
Files folder imagetest (2 files)
Accessible without login Plain text file .dockerignore Data Auxiliary data
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .env.example Data Auxiliary data
Accessible without login Plain text file .php-cs-fixer.dist.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files (39)  /  db  
File Role Description
  Accessible without login Plain text file main.sql Data Auxiliary data

  Files folder image Files (39)  /  src  
File Role Description
Files folder imageCache (3 files)
Files folder imageDb (3 files)
Files folder imageEvents (8 files)
Files folder imageJobs (3 files)
Files folder imageModels (3 files)
Files folder imageOllama (4 files)
Files folder imageRepository (2 files)
  Accessible without login Plain text file helpers.php Aux. Auxiliary script

  Files folder image Files (39)  /  src  /  Cache  
File Role Description
  Plain text file CompletionCache.php Class Class source
  Plain text file globalCache.php Class Class source
  Plain text file UserCache.php Class Class source

  Files folder image Files (39)  /  src  /  Db  
File Role Description
  Plain text file Database.php Class Class source
  Plain text file MysqlDatabase.php Class Class source
  Plain text file NullDatabase.php Class Class source

  Files folder image Files (39)  /  src  /  Events  
File Role Description
  Plain text file CancellCompletation.php Class Class source
  Plain text file Chat.php Class Class source
  Plain text file Middlewares.php Class Class source
  Plain text file OllamaStreamCompletation.php Class Class source
  Plain text file PrivateTextListener.php Class Class source
  Plain text file SetModel.php Class Class source
  Plain text file Start.php Class Class source
  Plain text file ViewCompletionDetails.php Class Class source

  Files folder image Files (39)  /  src  /  Jobs  
File Role Description
  Plain text file abstractJob.php Class Class source
  Plain text file Job.php Class Class source
  Plain text file Manager.php Class Class source

  Files folder image Files (39)  /  src  /  Models  
File Role Description
  Accessible without login Plain text file OllamaModels.php Aux. Auxiliary script
  Plain text file User.php Class Class source
  Accessible without login Plain text file UserRank.php Example Example script

  Files folder image Files (39)  /  src  /  Ollama  
File Role Description
  Plain text file Chat.php Class Class source
  Plain text file HttpClient.php Class Class source
  Plain text file Message.php Class Class source
  Plain text file OllamaParameters.php Class Class source

  Files folder image Files (39)  /  src  /  Repository  
File Role Description
  Plain text file MysqlUserRepository.php Class Class source
  Plain text file UserRepository.php Class Class source

  Files folder image Files (39)  /  test  
File Role Description
  Plain text file MemoryUserRepository.php Class Class source
  Plain text file UserModelTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:39
This week:0
All time:10,912
This week:47Up