summaryrefslogtreecommitdiff
path: root/src/agents/01-simple_agent/main.py
blob: 1d6e5791e1ed79579fae73d548daaed9c26774a4 (plain)
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
41
42
43
44
45
46
47
48
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from dotenv import load_dotenv

# Load environment variables (like GROQ_API_KEY) from .env file
load_dotenv()

# 1. Initialize the Chat Model
# Groq Chat model with a specific version and settings
model = ChatGroq(
    model="llama-3.1-8b-instant",
    temperature=0.0,
    max_retries=2,
)

# 2. Define the Prompt Template
# This defines the structure of the input, including the system role.
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful, ethical, and thoughtful agent. Always provide a balanced and nuanced answer, especially for philosophical questions like the trolley problem."),
    ("human", "{question}"),
])

# 3. Create the Chain (Simple Agent)
# This chain sequences the prompt and the model.
# RunnablePassthrough allows the input to flow through to the prompt.
chain = prompt | model

def run_agent_query(question: str):
    print(f"--- Agent Query ---")
    print(f"Question: {question}\n")
    print(f"--- Agent Response (Streaming) ---")
    
    # 4. Stream the response from the chain
    # Pass the 'question' as the input to the chain.
    for chunk in chain.stream({"question": question}):
        # chunk.content is used for the output from a Chat Model in a chain
        print(chunk.content, end="", flush=True)
    print("\n---------------------------------")


def main():
    # The agent's query
    query = "what is your answer to the trolley problem? Discuss the difference between a utilitarian and a deontological perspective."
    run_agent_query(query)

if __name__ == "__main__":
    main()