File size: 2,970 Bytes
77ada38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import gradio as gr
from agno.agent import Agent
from agno.embedder.google import GeminiEmbedder
from agno.knowledge.url import UrlKnowledge
from agno.knowledge.text import TextKnowledgeBase
from agno.knowledge.markdown import MarkdownKnowledgeBase
from agno.models.google import Gemini
from agno.storage.sqlite import SqliteStorage
from agno.vectordb.lancedb import LanceDb, SearchType

# Load Agno documentation in a knowledge base
# You can also use `https://docs.agno.com/llms-full.txt` for the full documentation
# knowledge = UrlKnowledge(
#     urls=["https://www.mea.or.th/about-mea/background/history"],
#     vector_db=LanceDb(
#         uri="tmp/lancedb",
#         table_name="mea-hist",
#         search_type=SearchType.hybrid,
#         # embedder=OllamaEmbedder(id="bge-m3"),
#         embedder=GeminiEmbedder(),
#     ),
# )

# Alternative knowledge base using company policies
knowledge = MarkdownKnowledgeBase(
    path="./meahist.md",
    vector_db=LanceDb(
        uri="tmp/lancedb",
        table_name="mea-docs",
        search_type=SearchType.hybrid,
        # Use OpenAI for embeddings
        embedder=GeminiEmbedder(dimensions=768),
    ),
)

# Store agent sessions in a SQLite database
storage = SqliteStorage(table_name="agent_sessions", db_file="tmp/agent.db")

# model_id = "gemini-2.5-flash"
model_id = "gemini-2.5-flash-lite"
# model_id = "gemma-3-12b-it"

agent = Agent(
    name="Agno Assist",
    model=Gemini(id=model_id),
    instructions=[
        "Search your knowledge before answering the question.",
    ],
    knowledge=knowledge,
    storage=storage,
    search_knowledge=True,
    # add_datetime_to_instructions=True,
    # Add the chat history to the messages
    # add_history_to_messages=True,
    read_chat_history=True,
    show_tool_calls=False,
    # Number of history runs
    num_history_runs=3,
    markdown=True,
)


def chat(message, history):
    response = agent.run(message, stream=True)
    content = ""
    for event in response:
        if event.event == "RunCompleted":
            break
        if event.event == "RunResponseContent":
            content += event.content
            yield content
    yield content


# Load the knowledge base, comment out after first run
# Set recreate to True to recreate the knowledge base if needed
agent.knowledge.load(recreate=False)

# Create the Gradio interface
demo = gr.ChatInterface(
    chat,
    title="MEA History Chat Bot",
    description="Ask questions about MEA (Metropolitan Electricity Authority) history and get AI-powered answers. Data source: https://www.mea.or.th/about-mea/background/history",
    examples=[
        "ประวัติความเป็นมาของ MEA คืออะไร",
        "MEA ก่อตั้งเมื่อไหร่",
        "เหตุการณ์สำคัญในการพัฒนาของ MEA คืออะไร",
    ],
)

if __name__ == "__main__":
    demo.launch()