1# You may need to add your working directory to the Python path. To do so, uncomment the following lines of code
2# import sys
3# sys.path.append("/Path/to/directory/bot-framework") # Replace with your directory path
4
5import logging
6
7from besser.bot.core.bot import Bot
8from besser.bot.core.session import Session
9from besser.bot.nlp.intent_classifier.intent_classifier_configuration import LLMIntentClassifierConfiguration
10from besser.bot.nlp.llm.llm_huggingface import LLMHuggingFace
11from besser.bot.nlp.llm.llm_huggingface_api import LLMHuggingFaceAPI
12from besser.bot.nlp.llm.llm_openai_api import LLMOpenAI
13from besser.bot.nlp.llm.llm_replicate_api import LLMReplicate
14
15logging.basicConfig(level=logging.INFO, format='{levelname} - {asctime}: {message}', style='{')
16
17# Create the bot
18bot = Bot('llm_bot')
19# Load bot properties stored in a dedicated file
20bot.load_properties('config.ini')
21# Define the platform your chatbot will use
22websocket_platform = bot.use_websocket_platform(use_ui=True)
23
24# Create the LLM
25gpt = LLMOpenAI(
26 bot=bot,
27 name='gpt-4o-mini',
28 parameters={},
29 num_previous_messages=10
30)
31
32# Other example LLM
33
34# gemma = LLMHuggingFace(bot=bot, name='google/gemma-2b-it', parameters={'max_new_tokens': 1}, num_previous_messages=10)
35# llama = LLMHuggingFaceAPI(bot=bot, name='meta-llama/Meta-Llama-3.1-8B-Instruct', parameters={}, num_previous_messages=10)
36# mixtral = LLMReplicate(bot=bot, name='mistralai/mixtral-8x7b-instruct-v0.1', parameters={}, num_previous_messages=10)
37
38ic_config = LLMIntentClassifierConfiguration(
39 llm_name='gpt-4o-mini',
40 parameters={},
41 use_intent_descriptions=True,
42 use_training_sentences=False,
43 use_entity_descriptions=True,
44 use_entity_synonyms=False
45)
46bot.set_default_ic_config(ic_config)
47
48# STATES
49
50greetings_state = bot.new_state('greetings_state', initial=True)
51answer_state = bot.new_state('answer_state')
52
53# INTENTS
54
55hello_intent = bot.new_intent(
56 name='hello_intent',
57 description='The user greets you'
58)
59
60maths_intent = bot.new_intent(
61 name='maths_intent',
62 description='The user asks something about mathematics'
63)
64
65physics_intent = bot.new_intent(
66 name='physics_intent',
67 description='The user asks something about physics'
68)
69
70literature_intent = bot.new_intent(
71 name='literature_intent',
72 description='The user asks something about literature'
73)
74
75psychology_intent = bot.new_intent(
76 name='psychology_intent',
77 description='The user asks something about psychology'
78)
79
80
81# STATES BODIES' DEFINITION + TRANSITIONS
82
83def global_fallback_body(session: Session):
84 answer = gpt.predict(f"You are being used within an intent-based chatbot. The chatbot triggered the fallback mechanism because no intent was recognized from the user input. Generate a message similar to 'Sorry, I don't know the answer', based on the user message: {session.message}")
85 session.reply(answer)
86
87
88bot.set_global_fallback_body(global_fallback_body)
89
90
91def greetings_body(session: Session):
92 answer = gpt.predict(f"You are a helpful assistant. Start the conversation with a short (2-15 words) greetings message. Make it original.")
93 session.reply(answer)
94
95
96greetings_state.set_body(greetings_body)
97# Here, we could create a state for each intent, but we keep it simple
98greetings_state.when_intent_matched_go_to(hello_intent, greetings_state)
99greetings_state.when_intent_matched_go_to(maths_intent, answer_state)
100greetings_state.when_intent_matched_go_to(physics_intent, answer_state)
101greetings_state.when_intent_matched_go_to(literature_intent, answer_state)
102greetings_state.when_intent_matched_go_to(psychology_intent, answer_state)
103
104
105def answer_body(session: Session):
106 answer = gpt.predict(session.message)
107 session.reply(answer)
108
109
110answer_state.set_body(answer_body)
111answer_state.go_to(greetings_state)
112
113# RUN APPLICATION
114
115if __name__ == '__main__':
116 bot.run()