From a5ed9f9e1c114eddb777cd1ffb3ff9e1b21a4c20 Mon Sep 17 00:00:00 2001 From: T-BENZIN Date: Tue, 17 Jun 2025 10:57:19 +0500 Subject: [PATCH 1/2] Applied changes made in GitHub --- reginaldCog/openai_completion.py | 27 +++++++++++++++++++-------- reginaldCog/tools_description.py | 21 ++++++++++++++------- reginaldCog/weather.py | 19 ++++++++----------- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/reginaldCog/openai_completion.py b/reginaldCog/openai_completion.py index 60aa384..c59502f 100644 --- a/reginaldCog/openai_completion.py +++ b/reginaldCog/openai_completion.py @@ -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()) + diff --git a/reginaldCog/tools_description.py b/reginaldCog/tools_description.py index 3d4558b..db8a01e 100644 --- a/reginaldCog/tools_description.py +++ b/reginaldCog/tools_description.py @@ -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 }, diff --git a/reginaldCog/weather.py b/reginaldCog/weather.py index 580716d..9bf0c7c 100644 --- a/reginaldCog/weather.py +++ b/reginaldCog/weather.py @@ -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) -- 2.47.2 From 4e22274f9b432e38f8586169dd471877211c99b9 Mon Sep 17 00:00:00 2001 From: T-BENZIN Date: Wed, 18 Jun 2025 20:10:05 +0500 Subject: [PATCH 2/2] - Removed unused debug import --- reginaldCog/openai_completion.py | 1 - reginaldCog/weather.py | 1 - 2 files changed, 2 deletions(-) diff --git a/reginaldCog/openai_completion.py b/reginaldCog/openai_completion.py index c59502f..e5eac5e 100644 --- a/reginaldCog/openai_completion.py +++ b/reginaldCog/openai_completion.py @@ -6,7 +6,6 @@ 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 CALLABLE_FUNCTIONS = { # Dictionary with functions to call. diff --git a/reginaldCog/weather.py b/reginaldCog/weather.py index 9bf0c7c..e4a1b32 100644 --- a/reginaldCog/weather.py +++ b/reginaldCog/weather.py @@ -2,7 +2,6 @@ from datetime import datetime, timezone from os import environ import requests import json -from debug_stuff import debug #WEATHER_API_KEY = environ.get('WEATHER_API_KEY') URL = 'http://api.weatherapi.com/v1' -- 2.47.2