Skip to content

Commit

Permalink
fix: Reddit tool credential warming
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendong-Fan committed Sep 10, 2024
1 parent f1035a7 commit 2b39b7f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ body:
attributes:
label: What version of camel are you using?
description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.
placeholder: E.g., 0.1.7.1
placeholder: E.g., 0.1.7.2
validations:
required: true

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ conda create --name camel python=3.10
conda activate camel
# Clone github repo
git clone -b v0.1.7.1 https://github.com/camel-ai/camel.git
git clone -b v0.1.7.2 https://github.com/camel-ai/camel.git
# Change directory into project directory
cd camel
Expand Down
2 changes: 1 addition & 1 deletion camel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========

__version__ = '0.1.7.1'
__version__ = '0.1.7.2'

__all__ = [
'__version__',
Expand Down
48 changes: 28 additions & 20 deletions camel/toolkits/reddit_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import os
import time
from typing import Any, Dict, List
from typing import Any, Dict, List, Union

from requests.exceptions import RequestException

Expand Down Expand Up @@ -50,20 +50,14 @@ def __init__(self, retries: int = 3, delay: int = 0):
self.retries = retries
self.delay = delay

client_id = os.environ.get("REDDIT_CLIENT_ID", "")
client_secret = os.environ.get("REDDIT_CLIENT_SECRET", "")
user_agent = os.environ.get("REDDIT_USER_AGENT", "")

if not all([client_id, client_secret, user_agent]):
print(
"Reddit API credentials are not set. "
"Please set the environment variables."
)
self.client_id = os.environ.get("REDDIT_CLIENT_ID", "")
self.client_secret = os.environ.get("REDDIT_CLIENT_SECRET", "")
self.user_agent = os.environ.get("REDDIT_USER_AGENT", "")

self.reddit = Reddit(
client_id=client_id,
client_secret=client_secret,
user_agent=user_agent,
client_id=self.client_id,
client_secret=self.client_secret,
user_agent=self.user_agent,
request_timeout=30, # Set a timeout to handle delays
)

Expand Down Expand Up @@ -96,7 +90,7 @@ def collect_top_posts(
subreddit_name: str,
post_limit: int = 5,
comment_limit: int = 5,
) -> List[Dict[str, Any]]:
) -> Union[List[Dict[str, Any]], str]:
r"""Collects the top posts and their comments from a specified
subreddit.
Expand All @@ -109,9 +103,16 @@ def collect_top_posts(
per post. Defaults to `5`.
Returns:
List[Dict[str, Any]]: A list of dictionaries, each containing the
post title and its top comments.
Union[List[Dict[str, Any]], str]: A list of dictionaries, each
containing the post title and its top comments if success.
String warming if credentials are not set.
"""
if not all([self.client_id, self.client_secret, self.user_agent]):
return (
"Reddit API credentials are not set. "
"Please set the environment variables."
)

subreddit = self._retry_request(self.reddit.subreddit, subreddit_name)
top_posts = self._retry_request(subreddit.top, limit=post_limit)
data = []
Expand Down Expand Up @@ -162,7 +163,7 @@ def track_keyword_discussions(
post_limit: int = 10,
comment_limit: int = 10,
sentiment_analysis: bool = False,
) -> List[Dict[str, Any]]:
) -> Union[List[Dict[str, Any]], str]:
r"""Tracks discussions about specific keywords in specified subreddits.
Args:
Expand All @@ -177,10 +178,17 @@ def track_keyword_discussions(
the comments. Defaults to `False`.
Returns:
List[Dict[str, Any]]: A list of dictionaries containing the
subreddit name, post title, comment body, and upvotes for each
comment that contains the specified keywords.
Union[List[Dict[str, Any]], str]: A list of dictionaries
containing the subreddit name, post title, comment body, and
upvotes for each comment that contains the specified keywords
if success. String warming if credentials are not set.
"""
if not all([self.client_id, self.client_secret, self.user_agent]):
return (
"Reddit API credentials are not set. "
"Please set the environment variables."
)

data = []

for subreddit_name in subreddits:
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
project = 'CAMEL'
copyright = '2023, CAMEL-AI.org'
author = 'CAMEL-AI.org'
release = '0.1.7.1'
release = '0.1.7.2'

html_favicon = (
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'
Expand Down
2 changes: 1 addition & 1 deletion docs/get_started/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ conda create --name camel python=3.10
conda activate camel
# Clone github repo
git clone -b v0.1.7.1 https://github.com/camel-ai/camel.git
git clone -b v0.1.7.2 https://github.com/camel-ai/camel.git
# Change directory into project directory
cd camel
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "camel-ai"
version = "0.1.7.1"
version = "0.1.7.2"
authors = ["CAMEL-AI.org"]
description = "Communicative Agents for AI Society Study"
readme = "README.md"
Expand Down

0 comments on commit 2b39b7f

Please sign in to comment.