Skip to content

feat: Added examples for anthropic api call-chat and function calling#183

Merged
dhruvj07 merged 1 commit intomainfrom
anthropic-api-univ-examples
Apr 14, 2025
Merged

feat: Added examples for anthropic api call-chat and function calling#183
dhruvj07 merged 1 commit intomainfrom
anthropic-api-univ-examples

Conversation

@dhruvj07
Copy link
Copy Markdown
Contributor

No description provided.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Hello @dhruvj07, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request adds two new example scripts for using the Javelin SDK with the Anthropic API. The first example, anthropic_api_function_calling.py, demonstrates how to use function calling with the Anthropic API. It defines a get_weather function and uses it to get the weather in Mumbai. The second example, javelin_anthropic_api_call.py, shows a basic API call to Anthropic's Claude model to get the three primary colors. Additionally, a minor change was made to javelin_azureopenai_univ_endpoint.py to include loading environment variables using load_dotenv().

Highlights

  • Function Calling Example: Added anthropic_api_function_calling.py to demonstrate function calling with the Anthropic API, including defining a tool and using input_schema.
  • Basic API Call Example: Added javelin_anthropic_api_call.py to show a basic API call to Anthropic's Claude model, including setting up headers and formatting the query body.
  • Environment Variable Loading: Modified javelin_azureopenai_univ_endpoint.py to load environment variables using load_dotenv().

Changelog

Click here to see the changelog
  • examples/anthropic/anthropic_api_function_calling.py
    • Created a new example script to demonstrate function calling with the Anthropic API.
    • Configured the Javelin client with necessary API keys and headers.
    • Defined a get_weather tool with an input_schema for function calling.
    • Constructed a query body with messages, tools, and tool choice.
    • Called the Javelin unified endpoint to execute the function call.
  • examples/anthropic/javelin_anthropic_api_call.py
    • Created a new example script to demonstrate a basic API call to Anthropic's Claude model.
    • Configured the Javelin client with necessary API keys and headers.
    • Formatted the query body to be compatible with Claude's messages format.
    • Called the Javelin unified endpoint to query the Claude model.
    • Added a helper function print_response to pretty print the response.
  • examples/azure-openai/javelin_azureopenai_univ_endpoint.py
    • Added load_dotenv() to load environment variables from a .env file.
    • Ensures that API keys and other configuration parameters can be set via environment variables.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


In code's vast domain,
Anthropic's call, a sweet refrain,
Functions now sing,
New examples bring,
A clearer path, again, again.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request adds examples for Anthropic API calls, including chat and function calling. The examples seem well-structured and provide a good starting point for users looking to integrate with the Javelin SDK. However, there are a few areas that could be improved for clarity and maintainability.

Summary of Findings

  • Missing Error Handling: The anthropic_api_function_calling.py example lacks explicit error handling for the API call. Adding a try-except block would make the example more robust.
  • Inconsistent Header Definitions: The header definitions are repeated across multiple files. Consider creating a shared function or constant to avoid duplication.
  • Clarity of Comments: Some comments could be more descriptive to explain the purpose of specific code sections, especially for those unfamiliar with the Javelin SDK.

Merge Readiness

The pull request introduces valuable examples for Anthropic API calls. However, addressing the missing error handling and inconsistent header definitions would significantly improve the code's robustness and maintainability. I recommend addressing these issues before merging. I am unable to approve this pull request, and users should have others review and approve this code before merging.

Comment on lines +61 to +67
# Call
response = client.query_unified_endpoint(
provider_name="anthropic",
endpoint_type="messages",
query_body=query_body,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a try-except block to handle potential exceptions during the API call. This will make the example more robust and provide better feedback in case of errors.

Suggested change
# Call
response = client.query_unified_endpoint(
provider_name="anthropic",
endpoint_type="messages",
query_body=query_body,
)
try:
response = client.query_unified_endpoint(
provider_name="anthropic",
endpoint_type="messages",
query_body=query_body,
)
print(response)
except Exception as e:
print(f"Error calling Anthropic API: {e}")

Comment on lines +50 to +57
response = client.query_unified_endpoint(
provider_name="anthropic",
endpoint_type="messages",
query_body=query_body,
)
print_response("Anthropic", response)
except Exception as e:
print(f"Anthropic query failed: {str(e)}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a more specific exception type to catch, such as httpx.HTTPError, to handle network-related issues more gracefully. Also, it would be helpful to log the exception details for debugging purposes.

except httpx.HTTPError as e:
    print(f"Anthropic query failed: {str(e)}")

Comment on lines +24 to +31
custom_headers = {
"Content-Type": "application/json",
"x-javelin-route": "anthropic_univ",
"x-javelin-model": "claude-3-5-sonnet-20240620",
"x-javelin-provider": "https://api.anthropic.com/v1",
"x-api-key": os.getenv("ANTHROPIC_API_KEY"), # For Anthropic model
"anthropic-version": "2023-06-01",
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

These headers are also defined in anthropic_api_function_calling.py. Consider defining them in a single place (e.g., a shared module or a configuration file) to avoid duplication and ensure consistency.

@dhruvj07 dhruvj07 merged commit f1c229a into main Apr 14, 2025
6 of 7 checks passed
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.

2 participants