mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-20 21:38:14 +00:00
Implement OAuth2 authentication support for custom LiteLLM providers
- Add OAuth2Config and OAuth2ConfigLoader for litellm_config.json configuration - Add OAuth2TokenManager for token acquisition, caching, and refresh - Extend LLM class to support OAuth2 authentication with custom providers - Add comprehensive tests covering OAuth2 flow and error handling - Add documentation and usage examples - Support Client Credentials OAuth2 flow for server-to-server authentication - Maintain backward compatibility with existing LLM providers Fixes #3114 Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
64
examples/oauth2_llm_example.py
Normal file
64
examples/oauth2_llm_example.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
Example demonstrating OAuth2 authentication with custom LLM providers in CrewAI.
|
||||
|
||||
This example shows how to configure and use OAuth2-authenticated LLM providers.
|
||||
"""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from crewai import Agent, Task, Crew, LLM
|
||||
|
||||
def create_example_config():
|
||||
"""Create an example OAuth2 configuration file."""
|
||||
config = {
|
||||
"oauth2_providers": {
|
||||
"my_custom_provider": {
|
||||
"client_id": "your_client_id_here",
|
||||
"client_secret": "your_client_secret_here",
|
||||
"token_url": "https://your-provider.com/oauth/token",
|
||||
"scope": "llm.read llm.write"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config_path = Path("example_oauth2_config.json")
|
||||
with open(config_path, 'w') as f:
|
||||
json.dump(config, f, indent=2)
|
||||
|
||||
print(f"Created example config at {config_path}")
|
||||
return config_path
|
||||
|
||||
def main():
|
||||
config_path = create_example_config()
|
||||
|
||||
try:
|
||||
llm = LLM(
|
||||
model="my_custom_provider/my-model",
|
||||
oauth2_config_path=str(config_path)
|
||||
)
|
||||
|
||||
agent = Agent(
|
||||
role="Research Assistant",
|
||||
goal="Provide helpful research insights",
|
||||
backstory="An AI assistant specialized in research and analysis",
|
||||
llm=llm
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Research the benefits of OAuth2 authentication in AI systems",
|
||||
agent=agent,
|
||||
expected_output="A comprehensive summary of OAuth2 benefits"
|
||||
)
|
||||
|
||||
crew = Crew(agents=[agent], tasks=[task])
|
||||
|
||||
print("Running crew with OAuth2-authenticated LLM...")
|
||||
result = crew.kickoff()
|
||||
print(f"Result: {result}")
|
||||
|
||||
finally:
|
||||
if config_path.exists():
|
||||
config_path.unlink()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user