8000 Merge pull request #2 from diangamichael/main · gjmulder/llama-cpp-python-gary@d059009 · GitHub
[go: up one dir, main page]

Skip to content

Commit d059009

Browse files
authored
Merge pull request #2 from diangamichael/main
Create telegram_typping.py
2 parents ba3959e + a379923 commit d059009

File tree

3 files changed

+244
-1
lines changed

3 files changed

+244
-1
lines changed

examples/tele-llama.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import os
2+
import time
3+
import logging
4+
from telethon import TelegramClient, events
5+
import requests
6+
import asyncio
7+
import aiohttp
8+
from telegram import ChatAction
9+
from telegram.ext import Updater, CommandHandler
10+
import requests
11+
# Set up logging
12+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13+
logger = logging.getLogger(__name__)
14+
15+
def get_current_model():
16+
url = f"http://{LLAMA_HOST}:8000/v1/models"
17+
headers = {'accept': 'application/json'}
18+
response = requests.get(url, headers=headers)
19+
20+
if response.status_code == 200:
21+
models_data = response.json()
22+
for model in models_data["data"]:
23+
if model["owned_by"] == "me":
24+
return model["id"]
25+
else:
26+
logger.error("Failed to fetch current model. Status code: %s", response.status_code)
27+
return None
28+
@client.on(events.NewMessage(pattern='(?i)/beth'))
29+
async def beth(event):
30+
global message_count
31+
32+
try:
33+
# Send typing action
34+
await client.send_chat_action(event.chat_id, ChatAction.TYPING)
35+
36+
# Check if message count has reached limit
37+
if message_count >= 1000:
38+
# Send "tired" message and return
39+
await event.respond("I'm hungover, I can't answer any more messages.")
40+
return
41+
42+
# Get the message text
43+
message_text = event.message.message.replace('/beth', '').strip().lower()
44+
parts = message_text.split(' ')
45+
logger.debug(f"Parts: {parts}")
46+
if len(parts) >= 2:
47+
try:
48+
temperature = float(parts[0])
49+
if 0.0 <= temperature <= 2.0:
50+
message_text = message_text.replace(f"{parts[0]} ", '')
51+
else:
52+
await event.respond("Too hot for me!")
53+
return
54+
except ValueError:
55+
temperature = 0.7
56+
else:
57+
temperature = 0.7
58+
59+
# Prepare the API request
60+
headers = {'accept': 'application/json', 'Content-Type': 'application/json'}
61+
data = {
62+
# "prompt": f"{message_text} Chess prodigy Beth Harmon overcomes addiction challenges male dominated world replies",
63+
"prompt": message_text,
64+
"temperature": temperature,
65+
"max_tokens": 50 # Adjust this value as needed
66+
}
67+
68+
# Log the user prompt
69+
logger.info(f"Temp: {temperature}, prompt: {message_text}")
70+
71+
# Record the time before the API request
72+
start_time = time.time()
73+
74+
# Send the API request
75+
api_url = f'http://{LLAMA_HOST}:8000/v1/completions'
76+
response = requests.post(api_url, headers=headers, json=data)
77+
78+
# Record the time after the API request
79+
end_time = time.time()
80+
message_count += 1
81+
82+
# Calculate the time difference
83+
time_difference = end_time - start_time
84+
minutes, seconds = divmod(time_difference, 60)
85+
86+
# Check if the request was successful
87+
if response.status_code == 200:
88+
# Parse the response and send the result to the chat
89+
api_result = response.json()
90+
result_text = api_result['choices'][0]['text'].strip()
91+
92+
# Add a default message if the result_text is empty
93+
if not result_text:
94+
result_text = "I'm sorry, but it is still your turn to move."
95+
96+
# Format the response time
97+
response_time = f"({int(minutes)}m{int(seconds)}s)"
98+
99+
# Add the response time to the result text
100+
result_text_with_time = f"{result_text} {response_time}"
101+
102+
# Log the API response
103+
logger.info(f"API response: {result_text_with_time}")
104+
105+
await client.send_message(event.chat_id, result_text_with_time)
106+
else:
107+
# Send an error message if the request was not successful
108+
await client.send_message(event.chat_id, "Sorry, I need to go to the bathroom. Back soon!")
109+
110+
except Exception as e:
111+
# Handle exceptions and send an error message
112+
logger.error(f"Error: {e}")
113+
await client.send_message(event.chat_id, "Oops. Broke the chess board.")
114+
115+
LLAMA_HOST = os.environ.get("LLAMA_HOST")
116+
117+
# Read the Telegram API credentials from environment variables
118+
API_ID = int(os.environ.get("API_ID"))
119+
API_HASH = os.environ.get("API_HASH")
120+
BOT_TOKEN = os.environ.get("BOT_TOKEN")
121+
122+
# Create a Telegram client
123+
client = TelegramClient('bot', API_ID, API_HASH).start(bot_token=BOT_TOKEN)
124+
125+
# Initialize global message count variable
126+
message_count = 0
127+
128+
current_model = get_current_model()
129+
if current_model:
130+
logger.info("Starting Telegram bot with Llama model: %s ", current_model)
131+
132+
# Start the Telegram client
133+
client.run_until_disconnected()

llama_cpp/telegram_typping.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import logging
2+
import asyncio
3+
import aiohttp
4+
from telegram import ChatAction
5+
from telegram.ext import Updater, CommandHandler
6+
7+
logging.basicConfig(
8+
level=logging.INFO,
9+
format='%(asctime)s %(levelname)s: %(message)s',
10+
handlers=[
11+
logging.FileHandler('bot.log'),
12+
logging.StreamHandler()
13+
]
14+
)
15+
16+
TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
17+
18+
async def fetch(url):
19+
async with aiohttp.ClientSession() as session:
20+
async with session.get(url) as response:
21+
return await response.text()
22+
23+
def start(update, context):
24+
update.message.reply_text("Hi! Type /simulate to simulate the bot typing.")
25+
logging.info("User %s started the bot", update.message.from_user.id)
26+
27+
async def simulate(update, context):
28+
chat_id = update.message.chat_id
29+
context.bot.send_chat_action(chat_id, ChatAction.TYPING)
30+
logging.info("Bot is typing a response...")
31+
32+
url = 'https://httpbin.org/get'
33+
logging.info("Fetching API response from %s", url)
34+
35+
loop = asyncio.get_event_loop()
36+
response_text = await fetch(url)
37+
38+
update.message.reply_text("API response received: " + response_text)
39+
logging.info("Sent response to user %s", update.message.from_user.id)
40+
41+
def stop(update, context):
42+
update.message.reply_text("Stopping the bot...")
43+
logging.info("User %s stopped the bot", update.message.from_user.id)
44+
context.bot.stop()
45+
46+
def main():
47+
updater = Updater(TOKEN, use_context=True)
48+
49+
dp = updater.dispatcher
50+
dp.add_handler(CommandHandler("start", start))
51+
dp.add_handler(CommandHandler("simulate", simulate))
52+
dp.add_handler(CommandHandler("stop", stop))
53+
54+
updater.start_polling()
55+
logging.info("Bot started polling for updates...")
56+
updater.bot.send_message(chat_id="@YOUR_CHANNEL_ID", text="Bot started.")
57+
updater.idle()
58+
updater.bot.send_message(chat_id="@YOUR_CHANNEL_ID", text="Bot stopped.")
59+
logging.info("Bot stopped polling for updates.")
60+
61+
if __name__ == "__main__":
62+
asyncio.run(main())

tests/test_llama.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,52 @@ def test_llama_pickle():
9393

9494
text = b"Hello World"
9595

96-
assert llama.detokenize(llama.tokenize(text)) == text
96+
assert llama.detokenize(llama.tokenize(text)) == text
97+
98+
99+
def test_model_output(llama):
100+
for prompt in PROMPTS:
101+
completion = llama.create_completion(prompt, max_tokens=20)
102+
assert "choices" in completion
103+
assert "text" in completion["choices"][0]
104+
assert isinstance(completion["choices"][0]["text"], str)
105+
106+
def test_temperature(llama):
107+
for prompt in PROMPTS:
108+
for temperature in TEMPERATURES:
109+
completion = llama.create_completion(prompt, max_tokens=20, temperature=temperature)
110+
assert "choices" in completion
111+
assert "text" in completion["choices"][0]
112+
assert isinstance(completion["choices"][0]["text"], str)
113+
114+
def test_top_k(llama):
115+
for prompt in PROMPTS:
116+
for top_k in TOP_K_VALUES:
117+
completion = llama.create_completion(prompt, max_tokens=20, top_k=top_k)
118+
assert "choices" in completion
119+
assert "text" in completion["choices"][0]
120+
assert isinstance(completion["choices"][0]["text"], str)
121+
122+
123+
def test_repetition_penalty(llama):
124+
for prompt in PROMPTS:
125+
for repetition_penalty in REPETITION_PENALTIES:
126+
completion = llama.create_completion(prompt, max_tokens=20, repetition_penalty=repetition_penalty)
127+
assert "choices" in completion
128+
assert "text" in completion["choices"][0]
129+
assert isinstance(completion["choices"][0]["text"], str)
130+
131+
def test_long_prompt(llama):
132+
prompt = "a" * (llama.get_max_sequence_length() + 1)
133+
completion = llama.create_completion(prompt, max_tokens=20)
134+
assert "choices" in completion
135+
assert "text" in completion["choices"][0]
136+
assert isinstance(completion["choices"][0]["text"], str)
137+
138+
def test_out_of_vocab(llama):
139+
prompt = "the quick brown fux"
140+
completion = llama.create_completion(prompt, max_tokens=20)
141+
assert "choices" in completion
142+
assert "text" in completion["choices"][0]
143+
assert isinstance(completion["choices"][0]["text"], str)
144+
assert "out of vocabulary" in completion["warning"]

0 commit comments

Comments
 (0)
0