LLM 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 besser.agent.core.agent import Agent
  8from besser.agent.core.session import Session
  9from besser.agent.exceptions.logger import logger
 10from besser.agent.nlp.intent_classifier.intent_classifier_configuration import LLMIntentClassifierConfiguration
 11from besser.agent.nlp.llm.llm_huggingface import LLMHuggingFace
 12from besser.agent.nlp.llm.llm_huggingface_api import LLMHuggingFaceAPI
 13from besser.agent.nlp.llm.llm_openai_api import LLMOpenAI
 14from besser.agent.nlp.llm.llm_replicate_api import LLMReplicate
 15
 16# Configure the logging module (optional)
 17logger.setLevel(logging.INFO)
 18
 19# Create the agent
 20agent = Agent('llm_agent')
 21# Load agent properties stored in a dedicated file
 22agent.load_properties('config.ini')
 23# Define the platform your agent will use
 24websocket_platform = agent.use_websocket_platform(use_ui=True)
 25
 26# Create the LLM
 27gpt = LLMOpenAI(
 28    agent=agent,
 29    name='gpt-4o-mini',
 30    parameters={},
 31    num_previous_messages=10
 32)
 33
 34# Other example LLM
 35
 36# gemma = LLMHuggingFace(agent=agent, name='google/gemma-2b-it', parameters={'max_new_tokens': 1}, num_previous_messages=10)
 37# llama = LLMHuggingFaceAPI(agent=agent, name='meta-llama/Meta-Llama-3.1-8B-Instruct', parameters={}, num_previous_messages=10)
 38# mixtral = LLMReplicate(agent=agent, name='mistralai/mixtral-8x7b-instruct-v0.1', parameters={}, num_previous_messages=10)
 39
 40ic_config = LLMIntentClassifierConfiguration(
 41    llm_name='gpt-4o-mini',
 42    parameters={},
 43    use_intent_descriptions=True,
 44    use_training_sentences=False,
 45    use_entity_descriptions=True,
 46    use_entity_synonyms=False
 47)
 48agent.set_default_ic_config(ic_config)
 49
 50# STATES
 51
 52greetings_state = agent.new_state('greetings_state', initial=True)
 53answer_state = agent.new_state('answer_state')
 54
 55# INTENTS
 56
 57hello_intent = agent.new_intent(
 58    name='hello_intent',
 59    description='The user greets you'
 60)
 61
 62maths_intent = agent.new_intent(
 63    name='maths_intent',
 64    description='The user asks something about mathematics'
 65)
 66
 67physics_intent = agent.new_intent(
 68    name='physics_intent',
 69    description='The user asks something about physics'
 70)
 71
 72literature_intent = agent.new_intent(
 73    name='literature_intent',
 74    description='The user asks something about literature'
 75)
 76
 77psychology_intent = agent.new_intent(
 78    name='psychology_intent',
 79    description='The user asks something about psychology'
 80)
 81
 82
 83# STATES BODIES' DEFINITION + TRANSITIONS
 84
 85def global_fallback_body(session: Session):
 86    answer = gpt.predict(f"You are being used within an intent-based agent. The agent 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.event.message}")
 87    session.reply(answer)
 88
 89
 90agent.set_global_fallback_body(global_fallback_body)
 91
 92
 93def greetings_body(session: Session):
 94    answer = gpt.predict(f"You are a helpful assistant. Start the conversation with a short (2-15 words) greetings message. Make it original.")
 95    session.reply(answer)
 96
 97
 98greetings_state.set_body(greetings_body)
 99# Here, we could create a state for each intent, but we keep it simple
100greetings_state.when_intent_matched(hello_intent).go_to(greetings_state)
101greetings_state.when_intent_matched(maths_intent).go_to(answer_state)
102greetings_state.when_intent_matched(physics_intent).go_to(answer_state)
103greetings_state.when_intent_matched(literature_intent).go_to(answer_state)
104greetings_state.when_intent_matched(psychology_intent).go_to(answer_state)
105
106
107def answer_body(session: Session):
108    answer = gpt.predict(session.event.message)
109    session.reply(answer)
110
111
112answer_state.set_body(answer_body)
113answer_state.go_to(greetings_state)
114
115# RUN APPLICATION
116
117if __name__ == '__main__':
118    agent.run()