Skip to content

Commit 3a85fa8

Browse files
committed
Ambrogio: Code improvements
Modified files: - llm_apps_with_memory_tutorials/ai_arxiv_agent_memory/ai_arxiv_agent_memory.py - advanced_tools_frameworks/cursor_ai_experiments/multi_agent_researcher.py - advanced_tools_frameworks/local_llama3.1_tool_use/llama3_tool_use.py - rag_tutorials/rag_chain/app.py - rag_tutorials/hybrid_search_rag/main.py
1 parent 1a036b2 commit 3a85fa8

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

advanced_tools_frameworks/cursor_ai_experiments/multi_agent_researcher.py

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
gpt4_model = None
88

99
def create_article_crew(topic):
10+
"""Creates a team of agents to research, write, and edit an article on a given topic.
11+
12+
This function sets up a crew consisting of three agents: a researcher, a writer, and an editor.
13+
Each agent is assigned a specific task to ensure the production of a well-researched,
14+
well-written, and polished article. The article is formatted using markdown standards.
15+
16+
Args:
17+
topic (str): The subject matter on which the article will be based.
18+
19+
Returns:
20+
Crew: A crew object that contains the agents and tasks necessary to complete the article."""
1021
# Create agents
1122
researcher = Agent(
1223
role='Researcher',

advanced_tools_frameworks/local_llama3.1_tool_use/llama3_tool_use.py

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
st.stop()
1414

1515
def get_assistant(tools):
16+
"""Creates and returns a configured assistant agent.
17+
18+
This function initializes an assistant agent with a specific model and toolset.
19+
The assistant is capable of accessing tools selected by the user and includes
20+
additional features such as showing tool call details, running in debug mode,
21+
and appending the current datetime to its instructions.
22+
23+
Args:
24+
tools (list): A list of tools that the assistant can access.
25+
26+
Returns:
27+
Agent: A configured assistant agent with specified capabilities and settings."""
1628
return Agent(
1729
name="llama3_assistant",
1830
model=Ollama(id="llama3.1:8b"),

llm_apps_with_memory_tutorials/ai_arxiv_agent_memory/ai_arxiv_agent_memory.py

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
search_query = st.text_input("Research paper search query")
3030

3131
def process_with_gpt4(result):
32+
"""Processes an arXiv search result to produce a structured markdown output.
33+
34+
This function takes a search result from arXiv and generates a markdown-formatted
35+
table containing details about each paper. The table includes columns for the
36+
paper's title, authors, a brief abstract, and a link to the paper on arXiv.
37+
38+
Args:
39+
result (str): The raw search result from arXiv, typically in a text format.
40+
41+
Returns:
42+
str: A markdown-formatted string containing a table with paper details."""
3243
prompt = f"""
3344
Based on the following arXiv search result, provide a proper structured output in markdown that is readable by the users.
3445
Each paper should have a title, authors, abstract, and link.

rag_tutorials/hybrid_search_rag/main.py

+40
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121
""".strip()
2222

2323
def initialize_config(openai_key: str, anthropic_key: str, cohere_key: str, db_url: str) -> RAGLiteConfig:
24+
"""Initializes and returns a RAGLiteConfig object with the specified API keys and database URL.
25+
26+
This function sets the provided API keys in the environment variables and returns a
27+
RAGLiteConfig object configured with the given database URL and pre-defined settings for
28+
language model, embedder, and reranker.
29+
30+
Args:
31+
openai_key (str): The API key for OpenAI services.
32+
anthropic_key (str): The API key for Anthropic services.
33+
cohere_key (str): The API key for Cohere services.
34+
db_url (str): The database URL for connecting to the desired data source.
35+
36+
Returns:
37+
RAGLiteConfig: A configuration object initialized with the specified parameters.
38+
39+
Raises:
40+
ValueError: If there is an issue setting up the configuration, an error is raised with details."""
2441
try:
2542
os.environ["OPENAI_API_KEY"] = openai_key
2643
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
@@ -39,6 +56,17 @@ def initialize_config(openai_key: str, anthropic_key: str, cohere_key: str, db_u
3956
raise ValueError(f"Configuration error: {e}")
4057

4158
def process_document(file_path: str) -> bool:
59+
"""Processes a document by inserting it into a system with a given configuration.
60+
61+
This function checks if a configuration is initialized in the session state.
62+
If the configuration is present, it attempts to insert the document located
63+
at the given file path using this configuration.
64+
65+
Args:
66+
file_path (str): The path to the document to be processed.
67+
68+
Returns:
69+
bool: True if the document was successfully processed; False otherwise."""
4270
try:
4371
if not st.session_state.get('my_config'):
4472
raise ValueError("Configuration not initialized")
@@ -49,6 +77,18 @@ def process_document(file_path: str) -> bool:
4977
return False
5078

5179
def perform_search(query: str) -> List[dict]:
80+
"""Conducts a hybrid search and returns a list of ranked chunks based on the query.
81+
82+
This function performs a search using a hybrid search method, retrieves the relevant
83+
chunks, and reranks them according to the query. It handles any exceptions that occur
84+
during the process and logs the errors.
85+
86+
Args:
87+
query (str): The search query string.
88+
89+
Returns:
90+
List[dict]: A list of dictionaries representing the ranked chunks. Returns an
91+
empty list if no results are found or if an error occurs."""
5292
try:
5393
chunk_ids, scores = hybrid_search(query, num_results=10, config=st.session_state.my_config)
5494
if not chunk_ids:

rag_tutorials/rag_chain/app.py

+50
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,29 @@
2020
persist_directory='./pharma_db')
2121

2222
def format_docs(docs):
23+
"""Formats a list of document objects into a single string.
24+
25+
Args:
26+
docs (list): A list of document objects, each having a 'page_content' attribute.
27+
28+
Returns:
29+
str: A single string containing the page content from each document,
30+
separated by double newlines."""
2331
return "\n\n".join(doc.page_content for doc in docs)
2432

2533
def add_to_db(uploaded_files):
34+
"""Processes and adds uploaded PDF files to the database.
35+
36+
This function checks if any files have been uploaded. If files are uploaded,
37+
it saves each file to a temporary location, processes the content using a PDF loader,
38+
and splits the content into smaller chunks. Each chunk, along with its metadata,
39+
is then added to the database. Temporary files are removed after processing.
40+
41+
Args:
42+
uploaded_files (list): A list of uploaded file objects to be processed.
43+
44+
Returns:
45+
None"""
2646
# Check if files are uploaded
2747
if not uploaded_files:
2848
st.error("No files uploaded!")
@@ -59,6 +79,18 @@ def add_to_db(uploaded_files):
5979
os.remove(temp_file_path)
6080

6181
def run_rag_chain(query):
82+
"""Processes a query using a Retrieval-Augmented Generation (RAG) chain.
83+
84+
This function utilizes a RAG chain to answer a given query. It retrieves
85+
relevant context using similarity search and then generates a response
86+
based on this context using a chat model. The chat model is pre-configured
87+
with a prompt template specialized in pharmaceutical sciences.
88+
89+
Args:
90+
query (str): The user's question that needs to be answered.
91+
92+
Returns:
93+
str: A response generated by the chat model, based on the retrieved context."""
6294
# Create a Retriever Object and apply Similarity Search
6395
retriever = db.as_retriever(search_type="similarity", search_kwargs={'k': 5})
6496

@@ -98,6 +130,24 @@ def run_rag_chain(query):
98130
return response
99131

100132
def main():
133+
"""Initialize and manage the PharmaQuery application interface.
134+
135+
This function sets up the Streamlit application interface for PharmaQuery,
136+
a Pharmaceutical Insight Retrieval System. Users can enter queries related
137+
to the pharmaceutical industry, upload research documents, and manage API
138+
keys for enhanced functionality.
139+
140+
The main features include:
141+
- Query input area for users to ask questions about the pharmaceutical industry.
142+
- Submission button to process the query and display the retrieved insights.
143+
- Sidebar for API key input and management.
144+
- File uploader for adding research documents to the database, enhancing query responses.
145+
146+
Args:
147+
None
148+
149+
Returns:
150+
None"""
101151
st.set_page_config(page_title="PharmaQuery", page_icon=":microscope:")
102152
st.header("Pharmaceutical Insight Retrieval System")
103153

0 commit comments

Comments
 (0)