Skip to content

Commit f7f10b4

Browse files
authored
Merge pull request #64 from programmatordev/OPA-59-ai-assistant-resource
New AI Assistant resource
2 parents 07386f7 + 410f7e3 commit f7f10b4

25 files changed

+413
-61
lines changed

docs/02-configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ $api = new OpenWeatherMap('yourapikey', [
7272
> To get to know about all the available methods, make sure to check the documentation [here](https://github.com/programmatordev/php-api-sdk?tab=readme-ov-file#documentation).
7373
7474
The following sections have examples of some of the most important methods,
75-
particularly related with the configuration of the client, cache and logger.
75+
particularly related to the configuration of the client, cache and logger.
7676

7777
### `setClientBuilder`
7878

docs/03-supported-apis.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- [getWeatherByDate](#getweatherbydate)
77
- [getWeatherSummaryByDate](#getweathersummarybydate)
88
- [getWeatherOverviewByDate](#getweatheroverviewbydate)
9+
- [AI Assistant](#ai-assistant)
10+
- [startSession](#startsession)
11+
- [resumeSession](#resumesession)
912
- [Weather](#weather)
1013
- [getCurrent](#getcurrent)
1114
- [getForecast](#getforecast)
@@ -84,6 +87,36 @@ Returns a [`WeatherOverview`](05-entities.md#weatheroverview) object:
8487
$weatherOverview = $api->oneCall()->getWeatherOverviewByDate(50, 50, new \DateTime('today'));
8588
```
8689

90+
### AI Assistant
91+
92+
#### `startSession`
93+
94+
```php
95+
startSession(string $prompt): Answer
96+
```
97+
98+
Start a new session (create a new conversation) with the Weather AI Assistant.
99+
100+
Returns a [`Answer`](05-entities.md#answer) object:
101+
102+
```php
103+
$answer = $api->assistant()->startSession('How is the weather today in Lisbon?');
104+
```
105+
106+
#### `resumeSession`
107+
108+
```php
109+
resumeSession(string $sessionId, string $prompt): Answer
110+
```
111+
112+
Resume a session (continue a conversation) with the Weather AI Assistant.
113+
114+
Returns a [`Answer`](05-entities.md#answer) object:
115+
116+
```php
117+
$answer = $api->assistant()->resumeSession('session-id', 'Do I need an umbrella?');
118+
```
119+
87120
### Weather
88121

89122
#### `getCurrent`
@@ -192,7 +225,7 @@ $locations = $api->geocoding()->getByLocationName('lisbon');
192225
getByCoordinate(float $latitude, float $longitude, int $numResults = 5): array
193226
```
194227

195-
Get name of the location (city name or area name) by using geographical coordinates (latitude, longitude).
228+
Get the name of the location (city name or area name) by using geographical coordinates (latitude, longitude).
196229

197230
Returns an array of [`Location`](05-entities.md#location) objects.
198231

@@ -261,7 +294,7 @@ withCacheTtl(?int $ttl): self
261294
Makes a request and saves into cache for the provided duration in seconds.
262295

263296
Semantics of values:
264-
- `0`, the response will not be cached (if the servers specifies no `max-age`).
297+
- `0`, the response will not be cached (if the server specifies no `max-age`).
265298
- `null`, the response will be cached for as long as it can (forever).
266299

267300
> [!NOTE]

docs/05-entities.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
- [Weather](#weather)
55
- [WeatherMoment](#weathermoment)
66
- [WeatherSummary](#weathersummary)
7+
- [WeatherOverview](#weatheroverview)
78
- [WeatherData](#weatherdata)
89
- [MinuteData](#minutedata)
910
- [HourData](#hourdata)
1011
- [DayData](#daydata)
1112
- [Alert](#alert)
1213
- [MoonPhase](#moonphase)
1314
- [Temperature](#temperature)
15+
- [AI Assistant](#ai-assistant)
16+
- [Answer](#answer)
17+
- [WeatherData](#weatherdata-1)
1418
- [Weather](#weather-1)
1519
- [Weather](#weather-2)
1620
- [WeatherCollection](#weathercollection)
17-
- [WeatherData](#weatherdata)
21+
- [WeatherData](#weatherdata-2)
1822
- [Air Pollution](#air-pollution)
1923
- [AirPollution](#airpollution)
2024
- [AirPollutionCollection](#airpollutioncollection)
@@ -171,6 +175,33 @@
171175
- `getMin()`: `?float`
172176
- `getMax()`: `?float`
173177

178+
## AI Assistant
179+
180+
### Answer
181+
182+
- `getAnswer()`: `string`
183+
- `getSessionId()`: `string`
184+
- `getData()`: [`WeatherData[]`](#weatherdata-1)
185+
186+
### WeatherData
187+
188+
- `getLocationName()`: `string`
189+
- `getDateTime()`: `\DateTimeImmutable`
190+
- `getTemperature()`: `float`
191+
- `getTemperatureFeelsLike()`: `float`
192+
- `getAtmosphericPressure()`: `int`
193+
- `getVisibility()`: `?int`
194+
- `getHumidity()`: `int`
195+
- `getDewPoint()`: `float`
196+
- `getUltraVioletIndex()`: `?float`
197+
- `getCloudiness()`: `int`
198+
- `getWind()`: [`Wind`](#wind)
199+
- `getConditions()`: [`Condition[]`](#condition)
200+
- `getRainVolume()`: `?float`
201+
- `getSnowVolume()`: `?float`
202+
- `getSunriseAt()`: `?\DateTimeImmutable`
203+
- `getSunsetAt()`: `?\DateTimeImmutable`
204+
174205
## Weather
175206

176207
### Weather

src/Entity/AirPollution/AirPollutionCollection.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
namespace ProgrammatorDev\OpenWeatherMap\Entity\AirPollution;
44

55
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
6-
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
6+
use ProgrammatorDev\OpenWeatherMap\Helper\EntityHelper;
77

88
class AirPollutionCollection
99
{
10-
use EntityTrait;
11-
1210
private int $numResults;
1311

1412
private Coordinate $coordinate;
@@ -20,7 +18,7 @@ public function __construct(array $data)
2018
{
2119
$this->numResults = count($data['list']);
2220
$this->coordinate = new Coordinate($data['coord']);
23-
$this->data = $this->createEntityList(AirPollutionData::class, $data['list']);
21+
$this->data = EntityHelper::createEntityList(AirPollutionData::class, $data['list']);
2422
}
2523

2624
public function getNumResults(): int

src/Entity/Assistant/Answer.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Assistant;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Helper\EntityHelper;
6+
7+
class Answer
8+
{
9+
private string $answer;
10+
11+
private string $sessionId;
12+
13+
/** @var WeatherData[] */
14+
private array $data = [];
15+
16+
public function __construct(array $data)
17+
{
18+
$this->answer = $data['answer'];
19+
$this->sessionId = $data['session_id'];
20+
21+
if (!empty($data['data'])) {
22+
$this->data = EntityHelper::createEntityKeyList(WeatherData::class, $data['data']);
23+
}
24+
}
25+
26+
public function getAnswer(): string
27+
{
28+
return $this->answer;
29+
}
30+
31+
public function getSessionId(): string
32+
{
33+
return $this->sessionId;
34+
}
35+
36+
public function getData(): array
37+
{
38+
return $this->data;
39+
}
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Assistant;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Entity\BaseWeather;
6+
7+
class WeatherData extends BaseWeather
8+
{
9+
private string $locationName;
10+
11+
private float $temperature;
12+
13+
private float $temperatureFeelsLike;
14+
15+
private int $visibility;
16+
17+
private \DateTimeImmutable $sunriseAt;
18+
19+
private \DateTimeImmutable $sunsetAt;
20+
21+
public function __construct(string $locationName, array $data)
22+
{
23+
parent::__construct($data);
24+
25+
$this->locationName = $locationName;
26+
$this->temperature = $data['temp'];
27+
$this->temperatureFeelsLike = $data['feels_like'];
28+
$this->visibility = $data['visibility'];
29+
$this->sunriseAt = \DateTimeImmutable::createFromFormat('U', $data['sunrise']);
30+
$this->sunsetAt = \DateTimeImmutable::createFromFormat('U', $data['sunset']);
31+
}
32+
33+
public function getLocationName(): string
34+
{
35+
return $this->locationName;
36+
}
37+
38+
public function getTemperature(): float
39+
{
40+
return $this->temperature;
41+
}
42+
43+
public function getTemperatureFeelsLike(): float
44+
{
45+
return $this->temperatureFeelsLike;
46+
}
47+
48+
public function getVisibility(): int
49+
{
50+
return $this->visibility;
51+
}
52+
53+
public function getSunriseAt(): \DateTimeImmutable
54+
{
55+
return $this->sunriseAt;
56+
}
57+
58+
public function getSunsetAt(): \DateTimeImmutable
59+
{
60+
return $this->sunsetAt;
61+
}
62+
}

src/Entity/OneCall/BaseWeather.php renamed to src/Entity/BaseWeather.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
<?php
22

3-
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity;
44

5-
use ProgrammatorDev\OpenWeatherMap\Entity\Condition;
6-
use ProgrammatorDev\OpenWeatherMap\Entity\Wind;
7-
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
5+
use ProgrammatorDev\OpenWeatherMap\Helper\EntityHelper;
86

97
class BaseWeather
108
{
11-
use EntityTrait;
12-
139
private \DateTimeImmutable $dateTime;
1410

1511
private int $atmosphericPressure;
@@ -46,7 +42,7 @@ public function __construct(array $data)
4642
'gust' => $data['wind_gust'] ?? null
4743
]);
4844

49-
$this->conditions = $this->createEntityList(Condition::class, $data['weather']);
45+
$this->conditions = EntityHelper::createEntityList(Condition::class, $data['weather']);
5046
$this->rainVolume = $data['rain']['1h'] ?? $data['rain']['3h'] ?? $data['rain'] ?? null;
5147
$this->snowVolume = $data['snow']['1h'] ?? $data['snow']['3h'] ?? $data['snow'] ?? null;
5248
}

src/Entity/OneCall/DayData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
44

5+
use ProgrammatorDev\OpenWeatherMap\Entity\BaseWeather;
6+
57
class DayData extends BaseWeather
68
{
79
private Temperature $temperature;

src/Entity/OneCall/HourData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
44

5+
use ProgrammatorDev\OpenWeatherMap\Entity\BaseWeather;
6+
57
class HourData extends BaseWeather
68
{
79
private float $temperature;

src/Entity/OneCall/Weather.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
66
use ProgrammatorDev\OpenWeatherMap\Entity\Timezone;
7-
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
7+
use ProgrammatorDev\OpenWeatherMap\Helper\EntityHelper;
88

99
class Weather
1010
{
11-
use EntityTrait;
12-
1311
private Coordinate $coordinate;
1412

1513
private Timezone $timezone;
@@ -43,14 +41,14 @@ public function __construct(array $data)
4341
$this->current = new WeatherData($data['current']);
4442

4543
$this->minutelyForecast = isset($data['minutely'])
46-
? $this->createEntityList(MinuteData::class, $data['minutely'])
44+
? EntityHelper::createEntityList(MinuteData::class, $data['minutely'])
4745
: null;
4846

49-
$this->hourlyForecast = $this->createEntityList(HourData::class, $data['hourly']);
50-
$this->dailyForecast = $this->createEntityList(DayData::class, $data['daily']);
47+
$this->hourlyForecast = EntityHelper::createEntityList(HourData::class, $data['hourly']);
48+
$this->dailyForecast = EntityHelper::createEntityList(DayData::class, $data['daily']);
5149

5250
$this->alerts = isset($data['alerts'])
53-
? $this->createEntityList(Alert::class, $data['alerts'])
51+
? EntityHelper::createEntityList(Alert::class, $data['alerts'])
5452
: null;
5553
}
5654

0 commit comments

Comments
 (0)