RAG agent#

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