File size: 2,094 Bytes
e5c3819 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# tools created using gemini
import json
import os
import google.generativeai as genai
from google.api_core import exceptions
# Retrieve API Key from Environment Variable
GOOGLE_AI_STUDIO = os.environ.get('GOOGLE_API_KEY')
# Ensure the API key is available
if not GOOGLE_AI_STUDIO:
raise ValueError("API key not found. Please set the GOOGLE_AI_STUDIO2 environment variable.")
import requests
from langchain.tools import tool
# Rest of your code remains the same
genai.configure(api_key=GOOGLE_AI_STUDIO)
model = genai.GenerativeModel('gemini-pro')
class GeminiSearchTools():
@tool("Gemini search the internet")
def gemini_search(query):
"""
Searches for content based on the provided query using the Gemini model.
Handles DeadlineExceeded exceptions from the Google API.
Args:
query (str): The search query.
Returns:
str: The response text from the Gemini model or an error message.
"""
try:
response = model.generate_content(query)
return response.text
except exceptions.DeadlineExceeded as e:
# Handle the DeadlineExceeded exception here
print("Error: Deadline Exceeded -", str(e))
# You can return a custom message or take other appropriate actions
return "Error: The request timed out. Please try again later."
@tool("Gemini search news on the internet")
def gemini_search_news(query):
"""
Searches for content based on the provided query using the Gemini model.
Handles DeadlineExceeded exceptions from the Google API.
Args:
query (str): The search query.
Returns:
str: The response text from the Gemini model or an error message.
"""
try:
response = model.generate_content(query)
return response.text
except exceptions.DeadlineExceeded as e:
# Handle the DeadlineExceeded exception here
print("Error: Deadline Exceeded -", str(e))
# You can return a custom message or take other appropriate actions
return "Error: The request timed out. Please try again later."
|