You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Hello! How to use adk web with non google api model? (Deepinfra or local) #1702
description ? or any code snippet ? (you can use LiteLLM library available in ADK)
I tried and I got an error after the first output when I used tools. This is my code:
from google.adk.agents import LlmAgent, Agent
from google.genai import types
import os
from google.adk.models.lite_llm import LiteLlm
from pathlib import Path
import praw
from dotenv import load_dotenv
load_dotenv(dotenv_path=Path('.') / '.env')
from .prompts import SYSTEM_INSTRUCTION
api_key = os.getenv("OPENROUTER_API_KEY")
def get_reddit_ai_news(subreddit: str, limit: int = 5) -> dict[str, list[str]]:
"""
Fetches top post titles from a specified subreddit using the Reddit API.
Args:
subreddit: The name of the subreddit to fetch news from (e.g., 'AINewsAndTrends').
limit: The maximum number of top posts to fetch.
Returns:
A dictionary with the subreddit name as key and a list of
post titles as value. Returns an error message if credentials are
missing, the subreddit is invalid, or an API error occurs.
"""
print(f"--- Tool called: Fetching from r/{subreddit} via Reddit API ---")
client_id = os.getenv("REDDIT_CLIENT_ID")
client_secret = os.getenv("REDDIT_CLIENT_SECRET")
user_agent = os.getenv("REDDIT_USER_AGENT")
if not all([client_id, client_secret, user_agent]):
print("--- Tool error: Reddit API credentials missing in .env file. ---")
return {subreddit: ["Error: Reddit API credentials not configured."]}
try:
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
user_agent=user_agent
)
# Check if the subreddit exists
reddit.subreddits.search_by_name(subreddit, exact=True)
sub = reddit.subreddit(subreddit)
top_posts = list(sub.hot(limit=limit)) # Fetch hot posts
titles = [post.title for post in top_posts]
if not titles:
return {subreddit: [f"No recent hot posts found in r/{subreddit}."]}
return {subreddit: titles}
except praw.exceptions.PRAWException as e:
print(f"--- Tool error: {e} ---")
return {subreddit: [f"Error fetching from r/{subreddit}: {str(e)}"]}
except Exception as e:
print(f"--- Tool error: {e} ---")
return {subreddit: [f"An unexpected error occurred: {str(e)}"]}
model = LiteLlm(
model="openrouter/mistralai/mistral-7b-instruct:free",
api_key=api_key
)
root_agent = LlmAgent(
name="reddit_news_agent",
description="Agent to help users find news articles on Reddit.",
model=model,
instruction=SYSTEM_INSTRUCTION,
tools=[get_reddit_ai_news],
output_key="summary"
)
Activity
omanandswami2005 commentedon Jun 28, 2025
description ? or any code snippet ? (you can use LiteLLM library available in ADK)
NewGenesis04 commentedon Jun 28, 2025
I tried and I got an error after the first output when I used tools. This is my code:
from google.adk.agents import LlmAgent, Agent
from google.genai import types
import os
from google.adk.models.lite_llm import LiteLlm
from pathlib import Path
import praw
from dotenv import load_dotenv
load_dotenv(dotenv_path=Path('.') / '.env')
from .prompts import SYSTEM_INSTRUCTION
api_key = os.getenv("OPENROUTER_API_KEY")
def get_reddit_ai_news(subreddit: str, limit: int = 5) -> dict[str, list[str]]:
"""
Fetches top post titles from a specified subreddit using the Reddit API.
model = LiteLlm(
model="openrouter/mistralai/mistral-7b-instruct:free",
api_key=api_key
)
root_agent = LlmAgent(
name="reddit_news_agent",
description="Agent to help users find news articles on Reddit.",
model=model,
instruction=SYSTEM_INSTRUCTION,
tools=[get_reddit_ai_news],
output_key="summary"
)
omanandswami2005 commentedon Jun 29, 2025
What was ur error? It's Working for me
Try pasting this code in same file in local installation of adk
boyangsvl commentedon Jun 30, 2025
Thanks for the discussions. I will move this to the discussions section since it doesn't seems to be an issue of ADK iteself.