File size: 1,332 Bytes
49e4e3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import logging
from config import MODEL_NAME
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain.agents import AgentExecutor
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents import AgentExecutor
from langchain.agents import create_tool_calling_agent
from langchain_core.utils.function_calling import convert_to_openai_function
from utils import book_slot, check_slots, reschedule_event, delete_event


load_dotenv()
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

API_KEY = os.environ["API_KEY"]
def create_agent(PROMPT):
    prompt_template = ChatPromptTemplate.from_messages([
        ("system", PROMPT),
        ("human", "{input}"),
        MessagesPlaceholder(variable_name="agent_scratchpad"),
    ])

    tools = [book_slot, delete_event, check_slots, reschedule_event]
    functions = [convert_to_openai_function(f) for f in tools]

    llm = ChatGroq(
        model=MODEL_NAME,
        temperature=0.7,
        max_tokens=None,
        timeout=None,
        max_retries=2,
        api_key=API_KEY
    ).bind_functions(functions=functions)

    agent = create_tool_calling_agent(llm, tools, prompt_template)
    agent_executor = AgentExecutor(agent=agent, tools=tools)
    return agent_executor