langchain-telegram-gpt-chatbot

Форк
0
216 строк · 6.8 Кб
1
import os
2
import openai
3
import requests
4
import telebot
5
import pickle
6
from langchain.vectorstores import FAISS as BaseFAISS
7

8
from dotenv import load_dotenv
9
from gtts import gTTS
10
from pydub import AudioSegment
11
from celery import Celery
12
import speech_recognition as sr
13

14
from langchain.embeddings import OpenAIEmbeddings
15

16
load_dotenv()
17

18
SYSTEM_PROMPT = os.getenv('SYSTEM_PROMPT')
19

20
app = Celery('chatbot', broker=os.getenv('CELERY_BROKER_URL'))
21

22
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
23

24
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
25

26
OPENAI_API_KEY = os.getenv('OPEN_AI_KEY')
27
MODEL_NAME = os.getenv('MODEL_NAME')
28

29
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
30

31
# Store the last 10 conversations for each user
32
conversations = {}
33

34

35
class FAISS(BaseFAISS):
36
    @staticmethod
37
    def load(file_path):
38
        with open(file_path, "rb") as f:
39
            return pickle.load(f)
40

41

42
# Load the FAISS index
43
faiss_obj_path = "models/" + MODEL_NAME + ".pickle"
44
faiss_index = FAISS.load(faiss_obj_path)
45

46

47
# @app.task
48
def generate_response_chat(message_list):
49
    if faiss_index:
50
        # Add extra text to the content of the last message
51
        last_message = message_list[-1]
52

53
        # Get the most similar documents to the last message
54
        try:
55
            docs = faiss_index.similarity_search(query=last_message["content"], k=2)
56

57
            updated_content = last_message["content"] + "\n\n"
58
            for doc in docs:
59
                updated_content += doc.page_content + "\n\n"
60
        except Exception as e:
61
            print(f"Error while fetching : {e}")
62
            updated_content = last_message["content"]
63

64
        print(updated_content)
65

66
        # Create a new HumanMessage object with the updated content
67
        # updated_message = HumanMessage(content=updated_content)
68
        updated_message = {"role": "user", "content": updated_content}
69

70
        # Replace the last message in message_list with the updated message
71
        message_list[-1] = updated_message
72

73
    openai.api_key = OPENAI_API_KEY
74
    # Send request to GPT-3 (replace with actual GPT-3 API call)
75
    gpt3_response = openai.ChatCompletion.create(
76
        model="gpt-4",
77
        temperature=0,
78
        messages=[
79
                     {"role": "system",
80
                      "content": SYSTEM_PROMPT},
81
                 ] + message_list
82
    )
83

84
    assistant_response = gpt3_response["choices"][0]["message"]["content"].strip()
85

86
    return assistant_response
87

88

89
def conversation_tracking(text_message, user_id):
90
    """
91
    Make remember all the conversation
92
    :param old_model: Open AI model
93
    :param user_id: telegram user id
94
    :param text_message: text message
95
    :return: str
96
    """
97
    # Get the last 10 conversations and responses for this user
98
    user_conversations = conversations.get(user_id, {'conversations': [], 'responses': []})
99
    user_messages = user_conversations['conversations'][-9:] + [text_message]
100
    user_responses = user_conversations['responses'][-9:]
101

102
    # Store the updated conversations and responses for this user
103
    conversations[user_id] = {'conversations': user_messages, 'responses': user_responses}
104

105
    # Construct the full conversation history in the user:assistant, " format
106
    conversation_history = []
107

108
    for i in range(min(len(user_messages), len(user_responses))):
109
        conversation_history.append({
110
            "role": "user", "content": user_messages[i]
111
        })
112
        conversation_history.append({
113
            "role": "assistant", "content": user_responses[i]
114
        })
115

116
    # Add last prompt
117
    conversation_history.append({
118
        "role": "user", "content": text_message
119
    })
120
    # Generate response
121
    response = generate_response_chat(conversation_history)
122
    # task = generate_response_chat.apply_async(args=[conversation_history])
123
    # response = task.get()
124

125
    # Add the response to the user's responses
126
    user_responses.append(response)
127

128
    # Store the updated conversations and responses for this user
129
    conversations[user_id] = {'conversations': user_messages, 'responses': user_responses}
130

131
    return response
132

133

134
@bot.message_handler(commands=["start", "help"])
135
def start(message):
136
    if message.text.startswith("/help"):
137
        bot.reply_to(message, "/clear - Clears old "
138
                              "conversations\nsend text to get replay\nsend voice to do voice"
139
                              "conversation")
140
    else:
141
        bot.reply_to(message, "Just start chatting to the AI or enter /help for other commands")
142

143

144
# Define a function to handle voice messages
145
@bot.message_handler(content_types=["voice"])
146
def handle_voice(message):
147
    user_id = message.chat.id
148
    # Download the voice message file from Telegram servers
149
    file_info = bot.get_file(message.voice.file_id)
150
    file = requests.get("https://api.telegram.org/file/bot{0}/{1}".format(
151
        TELEGRAM_BOT_TOKEN, file_info.file_path))
152

153
    # Save the file to disk
154
    with open("voice_message.ogg", "wb") as f:
155
        f.write(file.content)
156

157
    # Use pydub to read in the audio file and convert it to WAV format
158
    sound = AudioSegment.from_file("voice_message.ogg", format="ogg")
159
    sound.export("voice_message.wav", format="wav")
160

161
    # Use SpeechRecognition to transcribe the voice message
162
    r = sr.Recognizer()
163
    with sr.AudioFile("voice_message.wav") as source:
164
        audio_data = r.record(source)
165
        text = r.recognize_google(audio_data)
166

167
    # Generate response
168
    replay_text = conversation_tracking(text, user_id)
169

170
    # Send the question text back to the user
171
    # Send the transcribed text back to the user
172
    new_replay_text = "Human: " + text + "\n\n" + "sonic: " + replay_text
173

174
    bot.reply_to(message, new_replay_text)
175

176
    # Use Google Text-to-Speech to convert the text to speech
177
    tts = gTTS(replay_text)
178
    tts.save("voice_message.mp3")
179

180
    # Use pydub to convert the MP3 file to the OGG format
181
    sound = AudioSegment.from_mp3("voice_message.mp3")
182
    sound.export("voice_message_replay.ogg", format="mp3")
183

184
    # Send the transcribed text back to the user as a voice
185
    voice = open("voice_message_replay.ogg", "rb")
186
    bot.send_voice(message.chat.id, voice)
187
    voice.close()
188

189
    # Delete the temporary files
190
    os.remove("voice_message.ogg")
191
    os.remove("voice_message.wav")
192
    os.remove("voice_message.mp3")
193
    os.remove("voice_message_replay.ogg")
194

195

196
@bot.message_handler(func=lambda message: True)
197
def echo_message(message):
198
    user_id = message.chat.id
199

200
    # Handle /clear command
201
    if message.text == '/clear':
202
        conversations[user_id] = {'conversations': [], 'responses': []}
203
        bot.reply_to(message, "Conversations and responses cleared!")
204
        return
205

206
    response = conversation_tracking(message.text, user_id)
207

208
    # Reply to message
209
    bot.reply_to(message, response)
210

211

212
if __name__ == "__main__":
213
    print("Starting bot...")
214
    print("Bot Started")
215
    print("Press Ctrl + C to stop bot")
216
    bot.polling()
217

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.