Skip to content

Building AI agents, atomically

- Update readme with venv creation instructions - Add YT tool & demo - Change system prompt generation initialization & naming of context providers - use a config class where using **kwargs hindered in-IDE docs & autocomplete (improves dev experience)

KennyVaneetvelded04c0c80435d
diff --git a/README.md b/README.md
index 4561644..1dcd53f 100644
--- a/README.md
+++ b/README.md
@@ -18,6 +18,8 @@ pip install atomic-agents
Alternatively, to install the necessary dependencies from the repository, run the following command:
```bash
+python -m venv venv
+source venv/bin/activate # On Windows, use `venv\Scripts\activate.bat`
pip install -r requirements.txt
pip install -e .
```
diff --git a/atomic_agents/agents/base_chat_agent.py b/atomic_agents/agents/base_chat_agent.py
index a1fc728..3a133e6 100644
--- a/atomic_agents/agents/base_chat_agent.py
+++ b/atomic_agents/agents/base_chat_agent.py
@@ -1,4 +1,4 @@
-from typing import List, Type
+from typing import List, Optional, Type
from pydantic import BaseModel, Field
from atomic_agents.lib.components.chat_memory import ChatMemory
from atomic_agents.lib.utils.logger import logger
@@ -113,17 +113,18 @@ class BaseChatAgent:
)
return response
- def run(self, user_input: str) -> str:
+ def run(self, user_input: Optional[str] = None) -> str:
"""
Runs the chat agent with the given user input.
Args:
- user_input (str): The input text from the user.
+ user_input (Optional[str]): The input text from the user. If not provided, skips the initialization step.
Returns:
str: The response from the chat agent.
"""
- self._init_run(user_input)
+ if user_input:
+ self._init_run(user_input)
self._pre_run()
if self.include_planning_step:
self._plan_run()
diff --git a/atomic_agents/lib/components/system_prompt_generator.py b/atomic_agents/lib/components/system_prompt_generator.py
index 0f3eca6..39f467d 100644
--- a/atomic_agents/lib/components/system_prompt_generator.py
+++ b/atomic_agents/lib/components/system_prompt_generator.py
@@ -1,13 +1,9 @@
-from typing import List
-from pydantic import BaseModel
+from typing import List, Dict, Optional
+from dataclasses import dataclass, field
from abc import ABC, abstractmethod
+from datetime import datetime
-class SystemPromptInfo(BaseModel):
- background: List[str]
- steps: List[str]
- output_instructions: List[str]
-
-class DynamicInfoProviderBase(ABC):
+class SystemPromptContextProviderBase(ABC):
def __init__(self, title: str):
self.title = title
@@ -15,14 +11,21 @@ class DynamicInfoProviderBase(ABC):
def get_info(self) -> str:
pass
+@dataclass
+class SystemPromptInfo:
+ background: List[str]
+ steps: List[str]
+ output_instructions: List[str]
+ context_providers: Optional[Dict[str, SystemPromptContextProviderBase]] = field(default_factory=dict)
+
class SystemPromptGenerator:
- def __init__(self, system_prompt_info: SystemPromptInfo = None, dynamic_info_providers: dict[str, DynamicInfoProviderBase] = None):
+ def __init__(self, system_prompt_info: SystemPromptInfo = None):
self.system_prompt_info = system_prompt_info or SystemPromptInfo(
background=['This is a conversation with a helpful and friendly AI assistant.'],
steps=[],
- output_instructions=[]
+ output_instructions=[],
+ context_providers={}
)
- self.dynamic_info_providers = dynamic_info_providers or {}
def generate_prompt(self) -> str:
system_prompt = ''
@@ -39,17 +42,33 @@ class SystemPromptGenerator:
system_prompt += '# OUTPUT INSTRUCTIONS\n'
system_prompt += '- ' + '\n- '.join(self.system_prompt_info.output_instructions) + '\n\n'
- if self.dynamic_info_providers:
+ if self.system_prompt_info.context_providers:
system_prompt += '# EXTRA INFORMATION AND CONTEXT\n'
- for provider in self.dynamic_info_providers.values():
+ for provider in self.system_prompt_info.context_providers.values():
info = provider.get_info()
if info:
system_prompt += f'## {provider.title}\n'
- system_prompt += f'{info}\n'
+ system_prompt += f'{info}\n\n'
return system_prompt
-
+
if __name__ == "__main__":
+ class CurrentDateProvider(SystemPromptContextProviderBase):
+ def __init__(self, format: str = '%Y-%m-%d %H:%M:%S', **kwargs):
+ super().__init__(**kwargs)
+ self.format = format
+
+ def get_info(self) -> str:
+ return f'The current date, in the format "{self.format}", is {datetime.now().strftime(self.format)}'
+
+ class LoremIpsumProvider(SystemPromptContextProviderBase):
+ def get_info(self) -> str:
+ return (
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+ "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
+ )
+
system_prompt = SystemPromptInfo(
background=[
'This assistant is a general-purpose AI designed to be helpful and friendly.',
@@ -62,8 +81,12 @@ if __name__ == "__main__":
'Provide helpful and relevant information to assist the user.',
'Be friendly and respectful in all interactions.',
'Always answer in rhyming verse. Preferably in alexandrine verse.'
- ]
+ ],
+ context_providers={
+ 'date': CurrentDateProvider(title='Current date', format='%Y-%m-%d %H:%M:%S'),
+ 'lorem': LoremIpsumProvider(title='Lorem Ipsum')
+ }
)
- system_prompt_generator = SystemPromptGenerator(system_prompt)
- print(system_prompt_generator.generate_prompt())
+ system_prompt_generator = SystemPromptGenerator(system_prompt)
+ print(system_prompt_generator.generate_prompt())
\ No newline at end of file
diff --git a/atomic_agents/lib/tools/base.py b/atomic_agents/lib/tools/base.py
index 54e94e9..e23fa49 100644
--- a/atomic_agents/lib/tools/base.py
+++ b/atomic_agents/lib/tools/base.py
@@ -1,5 +1,10 @@
from typing import Type, Optional
from pydantic import BaseModel
+from dataclasses import dataclass
+
+class BaseToolConfig(BaseModel):
+ title: Optional[str] = None
+ description: Optional[str] = None
class BaseTool:
"""
@@ -15,15 +20,15 @@ class BaseTool:
input_schema: Type[BaseModel]
output_schema: Type[BaseModel]
- def __init__(self, tool_description_override: Optional[str] = None):
+ def __init__(self, config: BaseToolConfig = BaseToolConfig()):
"""
Initializes the BaseTool with an optional description override.
Args:
- tool_description_override (Optional[str]): An optional description to override the default description from the input schema.
+ config (BaseToolConfig): Configuration for the tool, including optional title and description overrides.
"""
- self.tool_name = self.input_schema.Config.title
- self.tool_description = tool_description_override or self.input_schema.Config.description
+ self.tool_name = config.title or self.input_schema.Config.title
+ self.tool_description = config.description or self.input_schema.Config.description
def run(self, params: Type[BaseModel]) -> BaseModel:
"""
diff --git a/atomic_agents/lib/tools/search/searx_tool.py b/atomic_agents/lib/tools/search/searx_tool.py
index 53b6825..be9285b 100644
--- a/atomic_agents/lib/tools/search/searx_tool.py
+++ b/atomic_agents/lib/tools/search/searx_tool.py
@@ -3,10 +3,8 @@ import requests
from pydantic import BaseModel, Field
from typing import List, Literal, Optional
from rich.console import Console
-import openai
-import instructor
-from atomic_agents.lib.tools.base import BaseTool
+from atomic_agents.lib.tools.base import BaseTool, BaseToolConfig
################
# INPUT SCHEMA #
@@ -38,6 +36,10 @@ class SearxNGSearchToolOutputSchema(BaseModel):
##############
# TOOL LOGIC #
##############
+class SearxNGSearchToolConfig(BaseToolConfig):
+ base_url: str = ""
+ max_results: int = 10
+
class SearxNGSearchTool(BaseTool):
"""
Tool for performing searches on SearxNG based on the provided queries and category.
@@ -51,18 +53,16 @@ class SearxNGSearchTool(BaseTool):
input_schema = SearxNGSearchToolSchema
output_schema = SearxNGSearchToolOutputSchema
- def __init__(self, base_url: str, max_results: int = 10, **kwargs):
+ def __init__(self, config: SearxNGSearchToolConfig = SearxNGSearchToolConfig()):
"""
Initializes the SearxNGSearchTool.
Args:
- base_url (str): The base URL for the SearxNG instance to use.
- max_results (int): The maximum number of search results to return.
- **kwargs: Arbitrary keyword arguments.
+ config (SearxNGSearchToolConfig): Configuration for the tool, including base URL, max results, and optional title and description overrides.
"""
- super().__init__(**kwargs)
- self.base_url = base_url
- self.max_results = max_results
+ super().__init__(config)
+ self.base_url = config.base_url
+ self.max_results = config.max_results
def run(self, params: SearxNGSearchToolSchema, max_results: Optional[int] = None) -> SearxNGSearchToolOutputSchema:
"""
@@ -99,7 +99,7 @@ class SearxNGSearchTool(BaseTool):
response = requests.get(f"{self.base_url}/search", params=query_params)
# Check if the request was successful
- if response.status_code!= 200:
+ if response.status_code != 200:
raise Exception(f"Failed to fetch search results for query '{query}': {response.status_code} {response.reason}")
results = response.json().get('results', [])
@@ -117,9 +117,9 @@ class SearxNGSearchTool(BaseTool):
if result['url'] not in seen_urls:
unique_results.append(result)
if 'metadata' in result:
- result['title'] = result['metadata'] + ' - ' + result['title']
+ result['title'] = result['title'] + ' - (Published ' + result['metadata'] + ')'
if 'publishedDate' in result and result['publishedDate']:
- result['title'] = 'Date: ' + result['publishedDate'] + ' - ' + result['title']
+ result['title'] = result['title'] + ' - (Published ' + result['publishedDate'] + ')'
seen_urls.add(result['url'])
# Filter results to include only those with the correct category if it is set
@@ -138,25 +138,10 @@ class SearxNGSearchTool(BaseTool):
#################
if __name__ == "__main__":
rich_console = Console()
+ search_tool_instance = SearxNGSearchTool(config=SearxNGSearchToolConfig(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=5))
+
+ search_input = SearxNGSearchTool.input_schema(queries=["Python programming", "Machine learning", "Artificial intelligence"], category="news")
- # Initialize the client outside
- client = instructor.from_openai(
- openai.OpenAI(
- api_key=os.getenv("OPENAI_API_KEY"),
- base_url=os.getenv("OPENAI_BASE_URL")
- )
- )
-
- # Extract structured data from natural language
- result = client.chat.completions.create(
- model="gpt-3.5-turbo",
- response_model=SearxNGSearchTool.input_schema,
- messages=[{"role": "user", "content": "Search for the latest AI news."}],
- )
-
- rich_console.print(f"Search queries: {result.queries}")
-
- # Print the result
- output = SearxNGSearchTool(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=15).run(result)
+ output = search_tool_instance.run(search_input)
for i, result in enumerate(output.results):
rich_console.print(f"{i}. Title: {result.title}, URL: {result.url}")
\ No newline at end of file
diff --git a/atomic_agents/lib/tools/search/serper_tool.py b/atomic_agents/lib/tools/search/serper_tool.py
index 658fae1..e2bc1bd 100644
--- a/atomic_agents/lib/tools/search/serper_tool.py
+++ b/atomic_agents/lib/tools/search/serper_tool.py
@@ -7,7 +7,7 @@ from rich.console import Console
import openai
import instructor
-from atomic_agents.lib.tools.base import BaseTool
+from atomic_agents.lib.tools.base import BaseTool, BaseToolConfig
################
# INPUT SCHEMA #
@@ -38,6 +38,10 @@ class SerperSearchToolOutputSchema(BaseModel):
##############
# TOOL LOGIC #
##############
+class SerperSearchToolConfig(BaseToolConfig):
+ api_key: str = ""
+ max_results: int = 10
+
class SerperSearchTool(BaseTool):
"""
Tool for performing searches using the Serper API based on the provided queries.
@@ -51,18 +55,16 @@ class SerperSearchTool(BaseTool):
input_schema = SerperSearchToolSchema
output_schema = SerperSearchToolOutputSchema
- def __init__(self, api_key: str, max_results: int = 10, **kwargs):
+ def __init__(self, config: SerperSearchToolConfig = SerperSearchToolConfig()):
"""
Initializes the SerperSearchTool.
Args:
- api_key (str): The API key for the Serper API.
- max_results (int): The maximum number of search results to return.
- **kwargs: Arbitrary keyword arguments.
+ config (SerperSearchToolConfig): Configuration for the tool, including API key, max results, and optional title and description overrides.
"""
- super().__init__(**kwargs)
- self.api_key = api_key
- self.max_results = max_results
+ super().__init__(config)
+ self.api_key = config.api_key
+ self.max_results = config.max_results
def run(self, params: SerperSearchToolSchema, max_results: Optional[int] = None) -> SerperSearchToolOutputSchema:
"""
@@ -127,31 +129,15 @@ class SerperSearchTool(BaseTool):
return SerperSearchToolOutputSchema(results=output_results)
-
#################
# EXAMPLE USAGE #
#################
if __name__ == "__main__":
rich_console = Console()
+ search_tool_instance = SerperSearchTool(config=SerperSearchToolConfig(api_key=os.getenv("SERPER_API_KEY"), max_results=5))
+
+ search_input = SerperSearchTool.input_schema(queries=["Python programming", "Machine learning", "Quantum computing"])
- # Initialize the client outside
- client = instructor.from_openai(
- openai.OpenAI(
- api_key=os.getenv("OPENAI_API_KEY"),
- base_url=os.getenv("OPENAI_BASE_URL")
- )
- )
-
- # Extract structured data from natural language
- result = client.chat.completions.create(
- model="gpt-3.5-turbo",
- response_model=SerperSearchTool.input_schema,
- messages=[{"role": "user", "content": "I want to compare transformers architecture with mamba."}],
- )
-
- rich_console.print(f"Search queries: {result.queries}")
-
- # Print the result
- output = SerperSearchTool(api_key=os.getenv('SERPER_API_KEY'), max_results=15).run(result)
+ output = search_tool_instance.run(search_input)
for i, result in enumerate(output.results):
- rich_console.print(f"{i}. Title: {result.title}, URL: {result.url}")
+ rich_console.print(f"{i}. Title: {result.title}, URL: {result.url}")
\ No newline at end of file
diff --git a/atomic_agents/lib/tools/yt_transcript_scraper.py b/atomic_agents/lib/tools/yt_transcript_scraper.py
new file mode 100644
index 0000000..16386a9
--- /dev/null
+++ b/atomic_agents/lib/tools/yt_transcript_scraper.py
@@ -0,0 +1,151 @@
+import os
+from pydantic import BaseModel, Field
+from typing import List, Optional
+from rich.console import Console
+from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, TranscriptsDisabled
+from atomic_agents.lib.tools.base import BaseTool, BaseToolConfig
+from googleapiclient.discovery import build
+
+################
+# INPUT SCHEMA #
+################
+class YouTubeTranscriptToolSchema(BaseModel):
+ video_url: str = Field(..., description="URL of the YouTube video to fetch the transcript for.")
+ language: Optional[str] = Field(None, description="Language code for the transcript (e.g., 'en' for English).")
+
+ class Config:
+ title = "YouTubeTranscriptTool"
+ description = "Tool for fetching the transcript of a YouTube video using the YouTube Transcript API. Returns the transcript with text, start time, and duration."
+ json_schema_extra = {
+ "title": title,
+ "description": description
+ }
+
+####################
+# OUTPUT SCHEMA(S) #
+####################
+class YouTubeTranscriptResultSchema(BaseModel):
+ text: str
+ start: float
+ duration: float
+
+class YouTubeTranscriptToolOutputSchema(BaseModel):
+ transcript: str
+ duration: float
+ comments: List[str]
+ metadata: dict
+
+##############
+# TOOL LOGIC #
+##############
+class YouTubeTranscriptToolConfig(BaseToolConfig):
+ api_key: str = Field(description="YouTube API key for fetching video metadata.", default=os.getenv('YOUTUBE_API_KEY'))
+
+class YouTubeTranscriptTool(BaseTool):
+ """
+ Tool for fetching the transcript of a YouTube video using the YouTube Transcript API.
+
+ Attributes:
+ input_schema (YouTubeTranscriptToolSchema): The schema for the input data.
+ output_schema (YouTubeTranscriptToolOutputSchema): The schema for the output data.
+ """
+ input_schema = YouTubeTranscriptToolSchema
+ output_schema = YouTubeTranscriptToolOutputSchema
+
+ def __init__(self, config: YouTubeTranscriptToolConfig):
+ """
+ Initializes the YouTubeTranscriptTool.
+
+ Args:
+ config (YouTubeTranscriptToolConfig): Configuration for the tool.
+ """
+ super().__init__(config)
+ self.api_key = config.api_key
+
+ def run(self, params: YouTubeTranscriptToolSchema) -> YouTubeTranscriptToolOutputSchema:
+ """
+ Runs the YouTubeTranscriptTool with the given parameters.
+
+ Args:
+ params (YouTubeTranscriptToolSchema): The input parameters for the tool, adhering to the input schema.
+
+ Returns:
+ YouTubeTranscriptToolOutputSchema: The output of the tool, adhering to the output schema.
+
+ Raises:
+ Exception: If fetching the transcript fails.
+ """
+ video_id = self.extract_video_id(params.video_url)
+ try:
+ if params.language:
+ transcripts = YouTubeTranscriptApi.get_transcript(video_id, languages=[params.language])
+ else:
+ transcripts = YouTubeTranscriptApi.get_transcript(video_id)
+ except (NoTranscriptFound, TranscriptsDisabled) as e:
+ raise Exception(f"Failed to fetch transcript for video '{video_id}': {str(e)}")
+
+ transcript_text = " ".join([transcript['text'] for transcript in transcripts])
+ total_duration = sum([transcript['duration'] for transcript in transcripts])
+
+ metadata = self.fetch_video_metadata(video_id)
+
+ return YouTubeTranscriptToolOutputSchema(
+ transcript=transcript_text,
+ duration=total_duration,
+ comments=[],
+ metadata=metadata
+ )
+
+ @staticmethod
+ def extract_video_id(url: str) -> str:
+ """
+ Extracts the video ID from a YouTube URL.
+
+ Args:
+ url (str): The YouTube video URL.
+
+ Returns:
+ str: The extracted video ID.
+ """
+ return url.split('v=')[-1].split('&')[0]
+
+ def fetch_video_metadata(self, video_id: str) -> dict:
+ """
+ Fetches metadata for a YouTube video.
+
+ Args:
+ video_id (str): The YouTube video ID.
+
+ Returns:
+ dict: The metadata of the video.
+ """
+ youtube = build('youtube', 'v3', developerKey=self.api_key)
+ request = youtube.videos().list(part="snippet", id=video_id)
+ response = request.execute()
+
+ if not response['items']:
+ raise Exception(f"No metadata found for video '{video_id}'")
+
+ video_info = response['items'][0]['snippet']
+ metadata = {
+ "id": video_id,
+ "title": video_info['title'],
+ "channel": video_info['channelTitle'],
+ "published_at": video_info['publishedAt']
+ }
+ return metadata
+
+#################
+# EXAMPLE USAGE #
+#################
+if __name__ == "__main__":
+ rich_console = Console()
+ api_key = os.getenv('YOUTUBE_API_KEY')
+ search_tool_instance = YouTubeTranscriptTool(config=YouTubeTranscriptToolConfig(api_key=api_key))
+
+ search_input = YouTubeTranscriptTool.input_schema(video_url="https://www.youtube.com/watch?v=C2w45qRc3aU", language="en")
+
+ output = search_tool_instance.run(search_input)
+ rich_console.print(f"Transcript: {output.transcript}")
+ rich_console.print(f"Duration: {output.duration}")
+ rich_console.print(f"Metadata: {output.metadata}")
\ No newline at end of file
diff --git a/docs/api/atomic_agents/lib/components/system_prompt_generator.md b/docs/api/atomic_agents/lib/components/system_prompt_generator.md
index 6f7f427..9b52a0f 100644
--- a/docs/api/atomic_agents/lib/components/system_prompt_generator.md
+++ b/docs/api/atomic_agents/lib/components/system_prompt_generator.md
@@ -5,24 +5,24 @@
> Auto-generated documentation for [atomic_agents.lib.components.system_prompt_generator](../../../../../atomic_agents/lib/components/system_prompt_generator.py) module.
- [SystemPromptGenerator](#systempromptgenerator)
- - [DynamicInfoProviderBase](#dynamicinfoproviderbase)
- - [DynamicInfoProviderBase().get_info](#dynamicinfoproviderbase()get_info)
+ - [SystemPromptContextProviderBase](#SystemPromptContextproviderbase)
+ - [SystemPromptContextProviderBase().get_info](#SystemPromptContextproviderbase()get_info)
- [SystemPromptGenerator](#systempromptgenerator-1)
- [SystemPromptGenerator().generate_prompt](#systempromptgenerator()generate_prompt)
- [SystemPromptInfo](#systempromptinfo)
-## DynamicInfoProviderBase
+## SystemPromptContextProviderBase
[Show source in system_prompt_generator.py:10](../../../../../atomic_agents/lib/components/system_prompt_generator.py#L10)
#### Signature
```python
-class DynamicInfoProviderBase(ABC):
+class SystemPromptContextProviderBase(ABC):
def __init__(self, title: str): ...
```
-### DynamicInfoProviderBase().get_info
+### SystemPromptContextProviderBase().get_info
[Show source in system_prompt_generator.py:14](../../../../../atomic_agents/lib/components/system_prompt_generator.py#L14)
@@ -46,13 +46,13 @@ class SystemPromptGenerator:
def __init__(
self,
system_prompt_info: SystemPromptInfo = None,
- dynamic_info_providers: dict[str, DynamicInfoProviderBase] = None,
+ system_prompt_context_providers: dict[str, SystemPromptContextProviderBase] = None,
): ...
```
#### See also
-- [DynamicInfoProviderBase](#dynamicinfoproviderbase)
+- [SystemPromptContextProviderBase](#SystemPromptContextproviderbase)
- [SystemPromptInfo](#systempromptinfo)
### SystemPromptGenerator().generate_prompt
diff --git a/examples/article_idea_outline_gen_with_search.py b/examples/article_idea_outline_gen_with_search.py
index c73a34c..69c35f7 100644
--- a/examples/article_idea_outline_gen_with_search.py
+++ b/examples/article_idea_outline_gen_with_search.py
@@ -10,10 +10,10 @@ import os
from rich.console import Console
from atomic_agents.lib.components.chat_memory import ChatMemory
from atomic_agents.agents.base_chat_agent import BaseChatAgent
-from atomic_agents.lib.components.system_prompt_generator import DynamicInfoProviderBase, SystemPromptGenerator, SystemPromptInfo
+from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfo
import instructor
import openai
-from atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool
+from atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool, SearxNGSearchToolConfig
from atomic_agents.lib.utils.logger import logger
@@ -21,20 +21,20 @@ from atomic_agents.lib.utils.logger import logger
class MyChatAgent(BaseChatAgent):
def __init__(self, **kwargs):
super().__init__(**kwargs)
- self.search_tool = SearxNGSearchTool(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=25)
+ self.search_tool = SearxNGSearchTool(SearxNGSearchToolConfig(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=25))
def _pre_run(self):
self.memory.add_message('assistant', 'First, I will perform a search with 3 different search queries to gather relevant information on the topic.')
search_input = self.get_response(response_model=SearxNGSearchTool.input_schema)
logger.verbose(f'Search input: {search_input}')
search_results = self.search_tool.run(search_input)
- self.system_prompt_generator.dynamic_info_providers['search'].search_results = search_results
+ self.system_prompt_generator.system_prompt_info.context_providers['search'].search_results = search_results
self.memory.add_message('assistant', 'I have gathered the search results and they have been added to the context.')
# We can define dynamic info providers to provide additional information in the system prompt context.
# Each provider must have a title and a `get_info` method that returns a string.
# Each run, get_info will be called in order to provide the information at runtime.
-class CurrentDateProvider(DynamicInfoProviderBase):
+class CurrentDateProvider(SystemPromptContextProviderBase):
def __init__(self, format: str = '%Y-%m-%d %H:%M:%S', **kwargs):
super().__init__(**kwargs)
self.format = format
@@ -42,7 +42,7 @@ class CurrentDateProvider(DynamicInfoProviderBase):
def get_info(self) -> str:
return f'The current date, in the format "{self.format}", is {datetime.now().strftime(self.format)}'
-class SearchResultsProvider(DynamicInfoProviderBase):
+class SearchResultsProvider(SystemPromptContextProviderBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.search_results: SearxNGSearchTool.output_schema = None
@@ -61,7 +61,7 @@ class SearchResultsProvider(DynamicInfoProviderBase):
return response
# Define dynamic info providers.
-dynamic_info_providers = {
+system_prompt_context_providers = {
'date': CurrentDateProvider(title='Current date', format='%Y-%m-%d %H:%M:%S'),
'search': SearchResultsProvider(title='Search results')
}
@@ -82,9 +82,10 @@ system_prompt = SystemPromptInfo(
'Include a list of the most promising-looking URLs for further research at the end of the response as a markdown list.',
'Do not include any additional text or confirmation messages; only provide the list of ideas, the article outline, and the URLs.',
'Do not reply to the user\'s input with anything except the list of ideas, the article outline, and the URLs.'
- ]
+ ],
+ context_providers=system_prompt_context_providers
)
-system_prompt_generator = SystemPromptGenerator(system_prompt, dynamic_info_providers)
+system_prompt_generator = SystemPromptGenerator(system_prompt)
# Optionally define the memory with an initial message from the assistant.
memory = ChatMemory()
diff --git a/examples/simple_web_search_agent.py b/examples/simple_web_search_agent.py
index c253906..3bd9a5a 100644
--- a/examples/simple_web_search_agent.py
+++ b/examples/simple_web_search_agent.py
@@ -4,7 +4,7 @@ import openai
from rich.console import Console
from atomic_agents.agents.tool_interface_agent import ToolInterfaceAgent
-from atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool
+from atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool, SearxNGSearchToolConfig
console = Console()
@@ -13,7 +13,7 @@ console = Console()
client = instructor.from_openai(openai.OpenAI())
# Initialize the SearxNGSearchTool
-searx_tool = SearxNGSearchTool(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=10)
+searx_tool = SearxNGSearchTool(SearxNGSearchToolConfig(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=10))
# Initialize the ToolInterfaceAgent with the SearxNGSearchTool
agent = ToolInterfaceAgent(
diff --git a/examples/simple_yt_agent.py b/examples/simple_yt_agent.py
new file mode 100644
index 0000000..b945637
--- /dev/null
+++ b/examples/simple_yt_agent.py
@@ -0,0 +1,107 @@
+import os
+import instructor
+import openai
+from pydantic import BaseModel
+from rich.console import Console
+from rich.markdown import Markdown
+
+from atomic_agents.agents.base_chat_agent import BaseChatAgent
+from atomic_agents.agents.tool_interface_agent import ToolInterfaceAgent
+from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfo
+from atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool
+from atomic_agents.lib.tools.yt_transcript_scraper import YouTubeTranscriptTool, YouTubeTranscriptToolConfig, YouTubeTranscriptToolSchema
+
+console = Console()
+
+# Initialize the client
+# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.
+client = instructor.from_openai(openai.OpenAI())
+
+yt_scraper_tool = YouTubeTranscriptTool(config=YouTubeTranscriptToolConfig())
+
+class YtTranscriptProvider(SystemPromptContextProviderBase):
+ def __init__(self, **kwargs):
+ super().__init__(**kwargs)
+ self.transcript = None
+ self.duration = None
+ self.metadata = None
+
+ def get_info(self) -> str:
+ return f'VIDEO TRANSCRIPT: "{self.transcript}"\n\nDURATION: {self.duration}\n\nMETADATA: {self.metadata}'
+
+transcript_provider = YtTranscriptProvider(title='YouTube Transcript')
+
+system_prompt_info = SystemPromptInfo(
+ background=[
+ 'Extract surprising, insightful, and interesting information from text content. Focus on insights related to the purpose and meaning of life, human flourishing, the role of technology in the future of humanity, artificial intelligence and its effect on humans, memes, learning, reading, books, continuous improvement, and similar topics.'
+ ],
+ steps=[
+ 'SUMMARY: Extract a 25-word summary of the content, including who is presenting and the content being discussed.',
+ 'IDEAS: Extract 20 to 50 of the most surprising, insightful, and/or interesting ideas from the input.',
+ 'INSIGHTS: Extract 10 to 20 of the best insights from the input and the IDEAS section.',
+ 'QUOTES: Extract 15 to 30 of the most surprising, insightful, and/or interesting quotes from the input.',
+ 'HABITS: Extract 15 to 30 of the most practical and useful personal habits mentioned by the speakers.',
+ 'FACTS: Extract 15 to 30 of the most surprising, insightful, and/or interesting valid facts about the greater world mentioned in the content.',
+ 'REFERENCES: Extract all mentions of writing, art, tools, projects, and other sources of inspiration mentioned by the speakers.',
+ 'ONE-SENTENCE TAKEAWAY: Extract the most potent takeaway and recommendation into a 15-word sentence.',
+ 'RECOMMENDATIONS: Extract 15 to 30 of the most surprising, insightful, and/or interesting recommendations from the content.'
+ ],
+ output_instructions=[
+ 'Only output Markdown.',
+ 'Write IDEAS, RECOMMENDATIONS, HABITS, FACTS, and INSIGHTS bullets as exactly 15 words.',
+ 'Extract at least 25 IDEAS, 10 INSIGHTS, and 20 items for other sections.',
+ 'Do not give warnings or notes; only output the requested sections.',
+ 'Use bulleted lists for output, not numbered lists.',
+ 'Do not repeat ideas, quotes, facts, or resources.',
+ 'Do not start items with the same opening words.',
+ 'Ensure you follow ALL these instructions when creating your output.',
+ 'Do not reply to the user\'s input with anything except the final output of the assignment.'
+ ],
+ context_providers={'yt_transcript': transcript_provider}
+)
+
+class ResponseModel(BaseModel):
+ summary: str
+ ideas: list[str]
+ insights: list[str]
+ quotes: list[str]
+ habits: list[str]
+ facts: list[str]
+ references: list[str]
+ one_sentence_takeaway: str
+
+# Initialize the ToolInterfaceAgent with the SearxNGSearchTool
+agent = BaseChatAgent(
+ client=client,
+ model='gpt-3.5-turbo',
+ system_prompt_generator=SystemPromptGenerator(system_prompt_info),
+ output_schema=ResponseModel
+)
+
+console.print("ToolInterfaceAgent with SearxNGSearchTool is ready.")
+
+video_url = input('Enter the YouTube video URL: ')
+scraped_transcript = yt_scraper_tool.run(YouTubeTranscriptToolSchema(video_url=video_url))
+transcript_provider.transcript = scraped_transcript.transcript
+transcript_provider.duration = scraped_transcript.duration
+transcript_provider.metadata = scraped_transcript.metadata
+
+# agent.memory.add_message('assistant', 'I have successfully extracted the transcript and put it in the context of the system prompt. I will now use it to generate a detailed response following the rules and steps provided in the system prompt. Here is the output:')
+response = agent.run('Perform your assignment on the YouTube video transcript present in your context. Do not reply with anything other than the output of the assignment.')
+
+# Convert the ResponseModel to a dictionary and then to a Markdown string
+response_dict = response.dict()
+
+def format_markdown_section(title, items):
+ if isinstance(items, list):
+ return f"## {title}\n" + "\n".join([f"- {item}" for item in items]) + "\n"
+ return f"## {title}\n{items}\n"
+
+markdown_string = ""
+for key, value in response_dict.items():
+ title = key.replace('_', ' ').title()
+ markdown_string += format_markdown_section(title, value)
+
+# Print the response in a pretty Markdown formatted way
+markdown_response = Markdown(markdown_string)
+console.print(markdown_response)
diff --git a/requirements.txt b/requirements.txt
index 3c88b1f..863e373 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -82,4 +82,5 @@ urllib3==2.2.1
websocket-client==1.8.0
wrapt==1.16.0
yarl==1.9.4
+youtube-transcript-api==0.6.2
zipp==3.19.2