Made improvements in AI Agent's tool get_forecast_weather #8
@ -1,10 +1,12 @@
|
||||
import random
|
||||
import json
|
||||
import asyncio
|
||||
from os import environ
|
||||
import openai
|
||||
from openai import OpenAIError
|
||||
from .weather import time_now, get_current_weather, get_weather_forecast
|
||||
from .tools_description import TOOLS
|
||||
from .debug_stuff import debug
|
||||
from weather import time_now, get_current_weather, get_weather_forecast
|
||||
from tools_description import TOOLS
|
||||
from debug_stuff import debug
|
||||
|
||||
CALLABLE_FUNCTIONS = {
|
||||
# Dictionary with functions to call.
|
||||
@ -21,15 +23,13 @@ class Completion:
|
||||
self.__api_key = api_key
|
||||
self.__messages = []
|
||||
|
||||
@debug
|
||||
async def create_completion(self, messages: list):
|
||||
self.__messages = messages
|
||||
async def create_completion(self):
|
||||
model = self.__model
|
||||
try:
|
||||
client = openai.AsyncClient(api_key=self.__api_key)
|
||||
completion_kwargs = {
|
||||
"model": model,
|
||||
"messages": messages,
|
||||
"messages": self.__messages,
|
||||
"max_tokens": 4096,
|
||||
"temperature": 0.7,
|
||||
"presence_penalty": 0.5,
|
||||
@ -47,7 +47,7 @@ class Completion:
|
||||
func_args = json.loads(i_call.function.arguments)
|
||||
tool_call_id = i_call.id
|
||||
self.function_manager(func_name, func_args, tool_call_id)
|
||||
return self.create_completion(messages=self.__messages)
|
||||
return await self.create_completion()
|
||||
return response_content
|
||||
except OpenAIError as e:
|
||||
return self.get_error_message(error_message=str(e), error_type="OpenAIError")
|
||||
@ -81,3 +81,14 @@ class Completion:
|
||||
result = CALLABLE_FUNCTIONS[func_name](**func_kwargs)
|
||||
self.append_message(role="tool", content=result, tool_call_id=tool_call_id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
async def main():
|
||||
test_message = input('Your input: ')
|
||||
completion = Completion(model='gpt-4.1-mini', api_key=environ.get('OPENAI_API_KEY'))
|
||||
completion.append_message(role='user', content=test_message)
|
||||
result = await completion.create_completion()
|
||||
print(result)
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
@ -1,11 +1,17 @@
|
||||
TOOLS = [
|
||||
# time_now
|
||||
{
|
||||
'type': 'function',
|
||||
'function': {
|
||||
'name': 'time_now',
|
||||
'description': 'Get current date and time in UTC timezone.',
|
||||
'description': '''
|
||||
Get current date and time in UTC timezone.
|
||||
Use this before get_weather_forecast function if user gave you date of forecast
|
||||
instead of number of days.
|
||||
''',
|
||||
}
|
||||
},
|
||||
# get_current_weather
|
||||
{
|
||||
'type': 'function',
|
||||
'function': {
|
||||
@ -32,6 +38,7 @@ TOOLS = [
|
||||
'strict': True
|
||||
}
|
||||
},
|
||||
# get_weather_forecast
|
||||
{
|
||||
'type': 'function',
|
||||
'function': {
|
||||
@ -41,7 +48,8 @@ TOOLS = [
|
||||
forecast and weather alert as json. The data is returned as a Forecast Object.
|
||||
Forecast object contains astronomy data, day weather forecast and hourly interval weather information
|
||||
for a given city.
|
||||
With a free weather API subscription, only up to three days of forecast can be requested.
|
||||
Number of days could be less, depending on the API subscription plan.
|
||||
With a free plan only up to three days of forecast would be returned.
|
||||
''',
|
||||
'parameters': {
|
||||
'type': 'object',
|
||||
@ -53,16 +61,15 @@ TOOLS = [
|
||||
e.g: "Copenhagen", or "Copenhagen, Louisiana, US", if needed specifying.
|
||||
'''
|
||||
},
|
||||
'dt': {
|
||||
'type': 'string',
|
||||
'days': {
|
||||
'type': 'integer',
|
||||
'description': '''
|
||||
The date up until to request the forecast in YYYY-MM-DD format.
|
||||
Check the **time_now** function first if you unsure which date it is.
|
||||
Number of days of forecast between 0 and 13, where 0 is only today, 1 up to tomorrow, etc.
|
||||
'''
|
||||
},
|
||||
},
|
||||
'required': [
|
||||
'location', 'dt'
|
||||
'location', 'days'
|
||||
],
|
||||
'additionalProperties': False
|
||||
},
|
||||
|
||||
@ -2,27 +2,27 @@ from datetime import datetime, timezone
|
||||
from os import environ
|
||||
import requests
|
||||
import json
|
||||
from .debug_stuff import debug
|
||||
from debug_stuff import debug
|
||||
|
||||
#WEATHER_API_KEY = environ.get('WEATHER_API_KEY')
|
||||
URL = 'http://api.weatherapi.com/v1'
|
||||
|
||||
|
||||
@debug
|
||||
def time_now() -> str:
|
||||
return str(datetime.now(timezone.utc))
|
||||
|
||||
|
||||
@debug
|
||||
def get_current_weather(location: str) -> str:
|
||||
weather = Weather(location=location)
|
||||
return json.dumps(weather.realtime())
|
||||
|
||||
|
||||
@debug
|
||||
def get_weather_forecast(location: str, days: int = 14, dt: str = '2025-03-24') -> str:
|
||||
def get_weather_forecast(location: str, days: int = None) -> str:
|
||||
days += 1
|
||||
days = max(1, days)
|
||||
days = min(14, days)
|
||||
weather = Weather(location=location)
|
||||
return json.dumps(weather.forecast(days=days, dt=dt))
|
||||
return json.dumps(weather.forecast(days=days))
|
||||
|
||||
|
||||
class Weather:
|
||||
@ -39,7 +39,6 @@ class Weather:
|
||||
response = requests.get(url=f'{URL}{method}', params=params)
|
||||
return response.json()
|
||||
|
||||
@debug
|
||||
def realtime(self):
|
||||
method = '/current.json'
|
||||
params = {
|
||||
@ -48,19 +47,17 @@ class Weather:
|
||||
}
|
||||
return self.make_request(method=method, params=params)
|
||||
|
||||
@debug
|
||||
def forecast(self, days: int = 14, dt: str = '2025-03-24'):
|
||||
def forecast(self, days: int = 14):
|
||||
method = '/forecast.json'
|
||||
params = {
|
||||
'key': self.api_key,
|
||||
'q': self.location,
|
||||
'days': days,
|
||||
'dt': dt,
|
||||
}
|
||||
return self.make_request(method=method, params=params)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_weather = Weather('Aqtobe')
|
||||
result = json.dumps(test_weather.forecast(days=14, dt='2025-03-24'), indent=2)
|
||||
result = json.dumps(test_weather.forecast(days=13), indent=2)
|
||||
print(result)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user