Releajing commited on
Commit
0fcca52
1 Parent(s): f0deaf8

Upload 3 files

Browse files
Files changed (3) hide show
  1. context.py +36 -0
  2. discord_api.py +64 -0
  3. zefir7b_api.py +40 -0
context.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ contexts = {}
2
+
3
+ def update_context(user_id, message, role):
4
+ if user_id not in contexts:
5
+ contexts[user_id] = []
6
+
7
+ if message.author.id == user_id:
8
+ role = "user"
9
+ else:
10
+ role = "assistant"
11
+
12
+ new_context_entry = {"role": role, "content": message.content}
13
+ contexts[user_id].append(new_context_entry)
14
+
15
+ if len(contexts[user_id]) > 5:
16
+ contexts[user_id] = contexts[user_id][-5:]
17
+
18
+ return contexts[user_id]
19
+
20
+ def format_prompt(context):
21
+ prompt = "Привет! Меня зовут Хелпер, и я ваш дружелюбный дроид-путеводитель по миру Звездных войн. Задавайте свои вопросы на русском языке, и я постараюсь помочь вам, используя свои знания из этой захватывающей вселенной. Я буду стараться отвечать на все ваши запросы максимально точно и полезно."
22
+ messages = [{"role": "system", "content": prompt}]
23
+ messages.extend(context)
24
+
25
+ return messages
26
+
27
+ def filter_response(response):
28
+ unwanted_phrases = ["unwanted_phrase1", "unwanted_phrase2"]
29
+ for phrase in unwanted_phrases:
30
+ if phrase in response:
31
+ return "I'm sorry, I can't provide an answer to that question."
32
+ return response
33
+
34
+ def post_process(response):
35
+ # Add your post-processing logic here
36
+ return response
discord_api.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import discord
2
+ import traceback
3
+
4
+ DISCORD_TOKEN = 'указать токен'
5
+
6
+ class MyClient(discord.Client):
7
+ def __init__(self, *args, **kwargs):
8
+ intents = discord.Intents.default()
9
+ intents.message_content = True
10
+ super().__init__(intents=intents, *args, **kwargs)
11
+
12
+ async def on_message(self, message):
13
+ print("Received message:", message.content)
14
+ try:
15
+ if message.author.id == self.user.id:
16
+ return
17
+
18
+ context = update_context(message.author.id, message, "user")
19
+ print("Updated context:", context)
20
+
21
+ messages = format_prompt(context)
22
+ print("Formatted prompt:", messages)
23
+
24
+ response = await call_huggingface_api(messages)
25
+ print("Received response:", response)
26
+
27
+ if response is not None and 'generated_text' in response[0]:
28
+ response_text = response[0]['generated_text']
29
+ print("Filtered response:", response_text)
30
+
31
+ response_text = filter_response(response_text)
32
+ print("Post-processed response:", response_text)
33
+
34
+ # Check if the response contains the user's question
35
+ if context[-1]["content"].lower() not in response_text.lower():
36
+ # If the response does not contain the user's question, clear the context and generate a new response
37
+ contexts[message.author.id] = []
38
+ context = update_context(message.author.id, message, "user")
39
+ messages = format_prompt(context)
40
+ response = await call_huggingface_api(messages)
41
+ response_text = response[0]['generated_text']
42
+ response_text = filter_response(response_text)
43
+ response_text = post_process(response_text)
44
+
45
+ last_bot_response = response_text.split("user:")[-1].strip()
46
+ print("Sending response:", last_bot_response)
47
+
48
+ if len(last_bot_response) > 2000:
49
+ chunks = [last_bot_response[i:i+2000] for i in range(0, len(last_bot_response), 2000)]
50
+ for chunk in chunks:
51
+ await message.channel.send(chunk)
52
+ else:
53
+ await message.channel.send(last_bot_response)
54
+ else:
55
+ print("Error generating text.")
56
+ await message.channel.send("Error generating text.")
57
+ except Exception as e:
58
+ print(f"Error in on_message: {e}")
59
+ print(traceback.format_exc())
60
+ await message.channel.send("An error occurred while processing your message. Please try again later.")
61
+
62
+ async def main():
63
+ client = MyClient()
64
+ await client.start(DISCORD_TOKEN)
zefir7b_api.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import aiohttp
2
+ import json
3
+ import traceback
4
+
5
+ HUGGINGFACE_TOKEN = 'указать токен'
6
+ MODEL = "HuggingFaceH4/zephyr-7b-beta"
7
+
8
+ async def call_huggingface_api(messages, temperature=0.95, top_k=50, top_p=0.9, num_return_sequences=1000):
9
+ url = f"https://api-inference.huggingface.co/models/{MODEL}"
10
+ headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
11
+ data = {
12
+ "inputs": "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages]),
13
+ "options": {
14
+ "temperature": temperature,
15
+ "top_k": top_k,
16
+ "top_p": top_p,
17
+ "max_length": 1000,
18
+ "no_repeat_ngram_size": 3,
19
+ "do_sample": True,
20
+ "num_return_sequences": num_return_sequences,
21
+ "no_repeat_ngram_size": 3,
22
+ "repetition_penalty": 1.2,
23
+ "length_penalty": 1.0,
24
+ "early_stopping": True,
25
+ }
26
+ }
27
+
28
+ try:
29
+ async with aiohttp.ClientSession() as session:
30
+ async with session.post(url, headers=headers, json=data) as resp:
31
+ if resp.status == 200:
32
+ response = await resp.json()
33
+ return response
34
+ else:
35
+ print(f"Error calling Hugging Face API: {resp.status}, {await resp.text()}")
36
+ return None
37
+ except Exception as e:
38
+ print(f"Error calling Hugging Face API: {e}")
39
+ print(traceback.format_exc())
40
+ return None