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 xAI Grok Integration #42

Closed
0xspacecreated opened this issue Dec 29, 2024 · 0 comments · Fixed by #45
Closed

Add xAI Grok Integration #42

0xspacecreated opened this issue Dec 29, 2024 · 0 comments · Fixed by #45
Assignees
Labels
feature New feature or request

Comments

@0xspacecreated
Copy link
Contributor

Issue Description

The AI Agent currently relies on OpenAI & Anthropic for language model tasks. To enhance robustness, scalability, and versatility, integrating xAI's Grok API is proposed. This integration will allow the agent to dynamically switch between OpenAI and xAI for text-based tasks, leveraging the unique capabilities of both providers without replacing existing functionality.


Specifications

  1. Integrate xAI's Grok API

    • Authentication Setup:
      • Generate an xAI API key via the xAI Console.
      • Store the API key securely using environment variables or a configuration management tool.
    • Supported Features:
      • Chat completion tasks using the grok-2-latest model.
      • Structured outputs and advanced configurations provided by Grok.
  2. Implement logic

    • Implement logic to dynamically switch between OpenAI, Anthropic and xAI Grok for tools.
    • Allow developers to specify the preferred provider or default to a specific one in the configuration.
  3. Extend Configuration

    • Add xAI-specific settings to the agent’s configuration file:
      class Settings:
          # OpenAI Settings
          OPENAI_API_KEY: str = "your-openai-api-key"
          OPENAI_MODEL: str = "gpt-4"
      
          # xAI Grok API Settings
          XAI_API_KEY: str = "your-xai-api-key"
          XAI_DEFAULT_MODEL: str = "grok-2-latest"
          XAI_USE_CASES: List[str] = ["chat", "structured_outputs"]
  4. Implement Grok API Function

    • Add a function to interact with the Grok API:
      import requests
      from typing import Dict
      
      def call_grok_api(prompt: str, model: str = "grok-2-latest", **kwargs: Dict) -> str:
          """
          Call the xAI Grok API for text-based tasks.
      
          Args:
              prompt (str): The user prompt to send to the model.
              model (str): The Grok model to use (default is "grok-2-latest").
              kwargs (dict): Additional parameters for the API call.
      
          Returns:
              str: The model's response.
          """
          headers = {
              "Authorization": f"Bearer {settings.XAI_API_KEY}",
              "Content-Type": "application/json"
          }
          payload = {
              "model": model,
              "messages": [{"role": "user", "content": prompt}],
              **kwargs
          }
          response = requests.post("https://api.x.ai/v1/chat/completions", headers=headers, json=payload)
          response.raise_for_status()
          return response.json()["choices"][0]["message"]["content"]
  5. Enhance Agent Workflow

    • Modify the task execution pipeline to incorporate Grok API calls:
      async def process_task(prompt: str, use_case: str = "chat") -> str:
          if use_case in settings.XAI_USE_CASES:
              return call_grok_api(prompt)
          else:
              return await call_openai_api(prompt)
  6. Documentation

    • Add clear documentation to the /docs dir for the integration:
      • Setting up xAI Grok API keys.
      • Configuring agent settings for xAI Grok.
      • Examples of making Grok API calls.

Step-by-Step Implementation

  1. Set Up xAI API Key

    • Generate the API key through the xAI Console.
    • Store the API key in an environment variable:
      export XAI_API_KEY="your-api-key-here"
  2. Implement API Integration

    • Add call_grok_api to handle text-based tasks via the Grok API.
  3. Dynamic API Selection

    • Update the task execution logic to determine when to use xAI Grok vs. OpenAI.
  4. Test Integration

    • Test Grok API calls with various text prompts.
    • Ensure fallbacks to OpenAI work seamlessly when needed.
  5. Update Documentation

    • Include setup and usage instructions for xAI Grok integration.

Acceptance Criteria

  • xAI Grok API is successfully integrated for text-based tasks.
  • Dynamic API selection works based on task configuration.
  • Existing OpenAI functionality remains unaffected.
  • Documentation provides clear guidance on setup and usage.
  • Comprehensive testing ensures reliability across workflows.

Estimated Complexity

Medium: Integrating a new API and ensuring seamless compatibility with the existing system while maintaining backward compatibility.

@0xspacecreated 0xspacecreated added the feature New feature or request label Dec 29, 2024
@0xspacecreated 0xspacecreated self-assigned this Jan 1, 2025
@0xspacecreated 0xspacecreated linked a pull request Jan 1, 2025 that will close this issue
@0xspacecreated 0xspacecreated mentioned this issue Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant