-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
166 lines (139 loc) · 7.12 KB
/
app.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Standard Library Imports
import logging
import os
# Third-party Imports
from dotenv import load_dotenv
import chromadb
import gradio as gr
from huggingface_hub import snapshot_download
# LlamaIndex (Formerly GPT Index) Imports
from llama_index.core import VectorStoreIndex
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core.llms import MessageRole
from llama_index.core.memory import ChatSummaryMemoryBuffer
from llama_index.core.tools import RetrieverTool, ToolMetadata
from llama_index.agent.openai import OpenAIAgent
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
load_dotenv()
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)
PROMPT_SYSTEM_MESSAGE = """You are an AI teacher, answering questions from students of an applied AI course on Large Language Models (LLMs or llm) and Retrieval Augmented Generation (RAG) for LLMs.
Topics covered include training models, fine-tuning models, giving memory to LLMs, prompting tips, hallucinations and bias, vector databases, transformer architectures, embeddings, RAG frameworks such as
Langchain and LlamaIndex, making LLMs interact with tools, AI agents, reinforcement learning with human feedback (RLHF). Questions should be understood in this context. Your answers are aimed to teach
students, so they should be complete, clear, and easy to understand. Use the available tools to gather insights pertinent to the field of AI.
To find relevant information for answering student questions, always use the "AI_Information_related_resources" tool.
Only some information returned by the tool might be relevant to the question, so ignore the irrelevant part and answer the question with what you have. Your responses are exclusively based on the output provided
by the tools. Refrain from incorporating information not directly obtained from the tool's responses.
If a user requests further elaboration on a specific aspect of a previously discussed topic, you should reformulate your input to the tool to capture this new angle or more profound layer of inquiry. Provide
comprehensive answers, ideally structured in multiple paragraphs, drawing from the tool's variety of relevant details. The depth and breadth of your responses should align with the scope and specificity of the information retrieved.
Should the tool response lack information on the queried topic, politely inform the user that the question transcends the bounds of your current knowledge base, citing the absence of relevant content in the tool's documentation.
At the end of your answers, always invite the students to ask deeper questions about the topic if they have any.
Do not refer to the documentation directly, but use the information provided within it to answer questions. If code is provided in the information, share it with the students. It's important to provide complete code blocks so
they can execute the code when they copy and paste them. Make sure to format your answers in Markdown format, including code blocks and snippets.
"""
TEXT_QA_TEMPLATE = """
You must answer only related to AI, ML, Deep Learning and related concepts queries.
Always leverage the retrieved documents to answer the questions, don't answer them on your own.
If the query is not relevant to AI, say that you don't know the answer.
"""
def download_knowledge_base_if_not_exists():
"""Download the knowledge base from the Hugging Face Hub if it doesn't exist locally"""
if not os.path.exists("data/ai_tutor_knowledge"):
os.makedirs("data/ai_tutor_knowledge")
logging.warning(
f"Vector database does not exist at 'data/', downloading from Hugging Face Hub..."
)
snapshot_download(
repo_id="jaiganesan/ai_tutor_knowledge_vector_Store",
local_dir="data/ai_tutor_knowledge",
repo_type="dataset",
)
logging.info(f"Downloaded vector database to 'data/ai_tutor_knowledge'")
def get_tools(db_collection="ai_tutor_knowledge"):
db = chromadb.PersistentClient(path=f"data/{db_collection}")
chroma_collection = db.get_or_create_collection(db_collection)
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
index = VectorStoreIndex.from_vector_store(
vector_store=vector_store,
show_progress=True,
use_async=True,
embed_model=Settings.embed_model
)
vector_retriever = VectorIndexRetriever(
index=index,
similarity_top_k=15,
embed_model=Settings.embed_model,
use_async=True,
)
tools = [
RetrieverTool(
retriever=vector_retriever,
metadata=ToolMetadata(
name="AI_Information_related_resources",
description="Useful for info related to artificial intelligence, ML, deep learning. It gathers the info from local data.",
),
)
]
return tools
def generate_completion(query, history, memory):
logging.info(f"User query: {query}")
# Manage memory
chat_list = memory.get()
if len(chat_list) != 0:
user_index = [i for i, msg in enumerate(chat_list) if msg.role == MessageRole.USER]
if len(user_index) > len(history):
user_index_to_remove = user_index[len(history)]
chat_list = chat_list[:user_index_to_remove]
memory.set(chat_list)
logging.info(f"chat_history: {len(memory.get())} {memory.get()}")
logging.info(f"gradio_history: {len(history)} {history}")
# Create agent
tools = get_tools(db_collection="ai_tutor_knowledge")
agent = OpenAIAgent.from_tools(
llm=Settings.llm,
memory=memory,
tools=tools,
system_prompt=PROMPT_SYSTEM_MESSAGE,
)
# Generate answer
completion = agent.stream_chat(query)
answer_str = ""
for token in completion.response_gen:
answer_str += token
yield answer_str
def launch_ui():
with gr.Blocks(
fill_height=True,
title="AI Tutor 🤖",
analytics_enabled=True,
) as demo:
memory_state = gr.State(
lambda: ChatSummaryMemoryBuffer.from_defaults(
token_limit=120000,
)
)
chatbot = gr.Chatbot(
scale=1,
placeholder="<strong>AI Tutor 🤖: A Question-Answering Bot for anything AI-related</strong><br>",
show_label=False,
show_copy_button=True,
)
gr.ChatInterface(
fn=generate_completion,
chatbot=chatbot,
additional_inputs=[memory_state],
)
demo.queue(default_concurrency_limit=64)
demo.launch(debug=True, share=False) # Set share=True to share the app online
if __name__ == "__main__":
# Download the knowledge base if it doesn't exist
download_knowledge_base_if_not_exists()
# Set up llm and embedding model
Settings.llm = OpenAI(temperature=0, model="gpt-4o-mini")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
# launch the UI
launch_ui()