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 langchain_community.embeddings import OpenAIEmbeddings
8from langchain_community.vectorstores import Chroma
9from langchain_text_splitters import RecursiveCharacterTextSplitter
10
11from besser.bot import nlp
12from besser.bot.core.bot import Bot
13from besser.bot.core.session import Session
14from besser.bot.nlp.llm.llm_huggingface_api import LLMHuggingFaceAPI
15from besser.bot.nlp.llm.llm_openai_api import LLMOpenAI
16from besser.bot.nlp.llm.llm_replicate_api import LLMReplicate
17from besser.bot.nlp.rag.rag import RAGMessage, RAG
18
19logging.basicConfig(level=logging.INFO, format='{levelname} - {asctime}: {message}', style='{')
20
21# Create the bot
22bot = Bot('rag_bot')
23# Load bot properties stored in a dedicated file
24bot.load_properties('config.ini')
25# Define the platform your chatbot will use
26websocket_platform = bot.use_websocket_platform(use_ui=True)
27
28# Create Vector Store (RAG's DB)
29vector_store: Chroma = Chroma(
30 embedding_function=OpenAIEmbeddings(openai_api_key=bot.get_property(nlp.OPENAI_API_KEY)),
31 persist_directory='vector_store'
32)
33# Create text splitter (RAG creates a vector for each chunk)
34splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
35# Create the LLM (for the answer generation)
36gpt = LLMOpenAI(
37 bot=bot,
38 name='gpt-4o-mini',
39 parameters={},
40 num_previous_messages=10
41)
42
43# Other example LLM
44
45# gemma = LLMHuggingFace(bot=bot, name='google/gemma-2b-it', parameters={'max_new_tokens': 1}, num_previous_messages=10)
46# llama = LLMHuggingFaceAPI(bot=bot, name='meta-llama/Meta-Llama-3.1-8B-Instruct', parameters={}, num_previous_messages=10)
47# mixtral = LLMReplicate(bot=bot, name='mistralai/mixtral-8x7b-instruct-v0.1', parameters={}, num_previous_messages=10)
48
49# Create the RAG
50rag = RAG(
51 bot=bot,
52 vector_store=vector_store,
53 splitter=splitter,
54 llm_name='gpt-4o-mini',
55 k=4,
56 num_previous_messages=0
57)
58# Uncomment to fill the DB
59# rag.load_pdfs('./pdfs')
60
61# STATES
62
63initial_state = bot.new_state('initial_state', initial=True)
64rag_state = bot.new_state('rag_state')
65
66
67# STATES BODIES' DEFINITION + TRANSITIONS
68
69def initial_body(session: Session):
70 session.reply('Hi!')
71
72
73initial_state.set_body(initial_body)
74initial_state.when_no_intent_matched_go_to(rag_state)
75
76
77def rag_body(session: Session):
78 rag_message: RAGMessage = session.run_rag()
79 websocket_platform.reply_rag(session, rag_message)
80
81
82rag_state.set_body(rag_body)
83rag_state.go_to(initial_state)
84
85
86# RUN APPLICATION
87
88if __name__ == '__main__':
89 bot.run()