Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lookup_tweet_by_id to X Toolkit #165

Merged
merged 2 commits into from
Nov 28, 2024
Merged

Add lookup_tweet_by_id to X Toolkit #165

merged 2 commits into from
Nov 28, 2024

Conversation

Spartee
Copy link
Contributor

@Spartee Spartee commented Nov 27, 2024

This PR introduces the lookup_tweet_by_id tool to the X toolkit, enabling users to retrieve tweet details by tweet ID. This enhancement extends the toolkit's capabilities, allowing for more comprehensive interactions with the X (Twitter) API.

Key Changes:

  • Added lookup_tweet_by_id Tool:

    • Implemented the lookup_tweet_by_id function in tools/tweets.py, which allows users to fetch tweet information using a tweet ID.
    • Included error handling for API response codes and expanded URLs in tweets to assist language models in avoiding hallucinations due to shortened URLs.
  • Enhanced Toolkit Structure:

    • Added several configuration files to the X toolkit to establish a standardized project structure, which in the future will be generated by arcade new. These include:
      • .pre-commit-config.yaml: Defines pre-commit hooks for code quality checks.
      • .ruff.toml: Configuration for the Ruff linter.
      • LICENSE: MIT License file for the toolkit.
      • Makefile: Contains common commands for building, testing, and linting the toolkit.
  • Updated Makefile:

    • Added make check-toolkits command to the top-level Makefile. This command runs code quality tools for each toolkit that contains a Makefile.

Additional Notes:

  • Tests:

    • Added unit tests for the new lookup_tweet_by_id tool in tests/test_tweets.py.
    • Included tests for the user lookup functionality in tests/test_users.py.
  • Linting and Code Quality:

    • Configured pre-commit hooks and Ruff linter to enforce code standards.
    • Updated the pyproject.toml file with development dependencies for testing and linting.

@Spartee Spartee requested a review from a team November 27, 2024 18:05
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this file be at the root? (I don't think it has any effect when it's deep in a dir path like this)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helps with make check from the toolkit's root

Comment on lines 24 to 31
.PHONY: clean-build
clean-build: ## clean build artifacts
rm -rf dist

.PHONY: clean-dist
clean-dist: ## Clean all built distributions
@echo "🗑️ Cleaning dist directory"
@rm -rf dist
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 seem duplicative?

@@ -1,17 +1,43 @@
[tool.poetry]
name = "arcade_x"
version = "0.1.0"
Copy link
Collaborator

@nbarbettini nbarbettini Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to bump this to 0.1.3 at least (0.1.2 is the current release on pypi)

Comment on lines 39 to 43
if response.status_code != 201:
raise ToolExecutionError(
f"Failed to post a tweet during execution of '{post_tweet.__name__}' tool. Request returned an error: {response.status_code} {response.text}"
error_message = response.text
raise ToolExecutionError( # noqa: TRY003
"Error posting tweet",
developer_message=f"Twitter API Error: {response.status_code} {error_message}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have started simply doing response.raise_for_status() which raises an exception that is caught and turned into a ToolExecutionError by the ToolExecutor. Let's discuss which pattern we want to use.

Comment on lines +137 to +141
raise RetryableToolError( # noqa: TRY003
"No keywords or phrases provided",
developer_message="Predicted inputs didn't contain any keywords or phrases",
additional_prompt_content="Please provide at least one keyword or phrase for search",
retry_after_ms=500, # Play nice with X API rate limits
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Comment on lines 75 to 79
except httpx.HTTPError as e:
raise ToolExecutionError( # noqa: TRY003
"Network error during user lookup",
developer_message=str(e),
) from e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be caught by ToolExecutor (one level up)

pre-commit = "^3.4.0"
tox = "^4.11.1"
ruff = "^0.7.4"
arcade-ai = {version = "0.1.*", extras = ["fastapi", "evals"]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extras should not be required (let's discuss)

Comment on lines 124 to 125
"keywords": ["Arcade AI"],
"phrases": [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected keyword & phrases should be flipped. Keywords should be list of tokenized words.

Looking back on the search_recent_tweets_by_keywords implementation, I think the keywords and phrases input params can be handled better. The evals seem be to telling us that the LM has difficulty deciphering between the differences of keywords and phrases.

"Arcade-AI is the go-to startup"
as keywords: ["Arcade", "AI", "is", "the", "go", "to", "startup"]
as phrase: ["Arcade-AI is the go-to startup"]


def get_tweet_url(tweet_id: str) -> str:
"""Get the URL of a tweet given its ID."""
return f"https://x.com/x/status/{tweet_id}"


def parse_search_recent_tweets_response(response_data: Any) -> dict:
def get_headers_with_token(context: ToolContext) -> dict[str, str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So altering dict in-place is not idiomatic?

url = f"https://api.x.com/2/users/by/username/{username}?user.fields=created_at,description,id,location,most_recent_tweet_id,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,verified_type,withheld,entities"
headers = get_headers_with_token(context)

user_fields = ",".join([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much cleaner


async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers, timeout=10)
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the update to error handling for tools I've been avoiding try/catch wrapped around API calls.

@echo "🚀 Bumping version in pyproject.toml"
poetry version patch

.PHONY: check
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make check doesn't pass the mypy portion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scratch that. after make install all is well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this help with the "unfollowed import" mypy error?

@nbarbettini nbarbettini merged commit bebfcab into main Nov 28, 2024
5 checks passed
@nbarbettini nbarbettini deleted the twitter-improvements branch November 28, 2024 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants