Skip to content

Commit eff218d

Browse files
committed
Adds chat service and agents
1 parent 9b6bc34 commit eff218d

File tree

74 files changed

+4363
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+4363
-391
lines changed

.env.example

+9-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ LOG_STACK=single
1919
LOG_DEPRECATIONS_CHANNEL=null
2020
LOG_LEVEL=debug
2121

22-
DB_CONNECTION=sqlite
23-
# DB_HOST=127.0.0.1
24-
# DB_PORT=3306
25-
# DB_DATABASE=laravel
26-
# DB_USERNAME=root
27-
# DB_PASSWORD=
22+
DB_CONNECTION=pgsql
23+
DB_HOST=pgsql
24+
DB_PORT=5432
25+
DB_DATABASE=laravel
26+
DB_USERNAME=sail
27+
DB_PASSWORD=password
2828

2929
SESSION_DRIVER=database
3030
SESSION_LIFETIME=120
@@ -62,3 +62,6 @@ AWS_BUCKET=
6262
AWS_USE_PATH_STYLE_ENDPOINT=false
6363

6464
VITE_APP_NAME="${APP_NAME}"
65+
66+
OPENAI_API_KEY=
67+
OPENAI_ORGANIZATION=

Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
up:
2+
./vendor/bin/sail up -d
3+
./vendor/bin/sail exec laravel.test php artisan migrate
4+
5+
down:
6+
./vendor/bin/sail down
7+
8+
restart:
9+
./vendor/bin/sail down
10+
./vendor/bin/sail up -d
11+
12+
chat:
13+
./vendor/bin/sail artisan chat
14+
15+
bash:
16+
./vendor/bin/sail exec laravel.test /bin/bash

README.md

+160-44
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,182 @@
1-
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400" alt="Laravel Logo"></a></p>
1+
# LLM Agents Sample App - Laravel edition
22

3-
<p align="center">
4-
<a href="https://github.com/laravel/framework/actions"><img src="https://github.com/laravel/framework/workflows/tests/badge.svg" alt="Build Status"></a>
5-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
6-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
7-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
8-
</p>
3+
This sample application demonstrates the practical implementation and usage patterns of the LLM Agents library.
94

10-
## About Laravel
5+
> For more information about the LLM Agents package and its capabilities, please refer to
6+
> the [LLM Agents documentation](https://github.com/llm-agents-php/agents).
117
12-
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
8+
It provides a CLI interface to interact with various AI agents, showcasing the power and flexibility of the LLM Agents
9+
package.
1310

14-
- [Simple, fast routing engine](https://laravel.com/docs/routing).
15-
- [Powerful dependency injection container](https://laravel.com/docs/container).
16-
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
17-
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
18-
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
19-
- [Robust background job processing](https://laravel.com/docs/queues).
20-
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
11+
![image](https://github.com/user-attachments/assets/53104067-d3df-4983-8a59-435708f2b70c)
2112

22-
Laravel is accessible, powerful, and provides tools required for large, robust applications.
13+
## Features
2314

24-
## Learning Laravel
15+
- Multiple pre-configured AI agents with different capabilities
16+
- CLI interface for easy interaction with agents
17+
- Integration with OpenAI's GPT models
18+
- Database support for session persistence
2519

26-
Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
20+
## Prerequisites
2721

28-
You may also try the [Laravel Bootcamp](https://bootcamp.laravel.com), where you will be guided through building a modern Laravel application from scratch.
22+
- PHP 8.3 or higher
23+
- Composer
24+
- Git
25+
- OpenAI API key
2926

30-
If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
27+
## Quick Start with Docker
3128

32-
## Laravel Sponsors
29+
The easiest way to run the app is using our pre-built Docker image.
3330

34-
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the [Laravel Partners program](https://partners.laravel.com).
31+
**Follow these steps to get started:**
3532

36-
### Premium Partners
33+
1. Make sure you have Docker installed on your system.
3734

38-
- **[Vehikl](https://vehikl.com/)**
39-
- **[Tighten Co.](https://tighten.co)**
40-
- **[WebReinvent](https://webreinvent.com/)**
41-
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
42-
- **[64 Robots](https://64robots.com)**
43-
- **[Curotec](https://www.curotec.com/services/technologies/laravel/)**
44-
- **[Cyber-Duck](https://cyber-duck.co.uk)**
45-
- **[DevSquad](https://devsquad.com/hire-laravel-developers)**
46-
- **[Jump24](https://jump24.co.uk)**
47-
- **[Redberry](https://redberry.international/laravel/)**
48-
- **[Active Logic](https://activelogic.com)**
49-
- **[byte5](https://byte5.de)**
50-
- **[OP.GG](https://op.gg)**
35+
2. Create a `.env` file in the project root directory:
5136

52-
## Contributing
37+
```bash
38+
cp .env.example .env
39+
```
40+
41+
and add your OpenAI API key to the `.env` file:
42+
43+
```bash
44+
OPENAI_API_KEY=your_api_key_here
45+
```
46+
47+
> Replace `<your_api_key>` with your OpenAI API key.
48+
49+
3. Run the Docker container with the following command:
50+
51+
```bash
52+
make up
53+
```
54+
55+
4Once the container is running, you can interact with the app using the following command:
56+
57+
## Usage
58+
59+
### Chatting with Agents
60+
61+
To start a chat session with an AI agent:
62+
63+
1. Run the following command:
64+
65+
**Using docker container**
66+
67+
```bash
68+
make chat
69+
```
70+
71+
2. You will see a list of available agents and their descriptions. Choose the desired agent by entering its number.
72+
73+
![image](https://github.com/user-attachments/assets/3cd223a8-3ab0-4879-9e85-83539c93003f)
74+
75+
3. After selecting an agent, you will see a message like this:
76+
77+
![image](https://github.com/user-attachments/assets/0d18ca6c-9ee9-4942-b383-fc42abf18bc7)
78+
79+
```bash
80+
************************************************************
81+
* Run the following command to see the AI response *
82+
************************************************************
83+
84+
php artisan chat:session <session_uuid> -v
85+
```
86+
87+
**Using docker container**
5388

54-
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
89+
```bash
90+
make bash
91+
```
5592

56-
## Code of Conduct
93+
Then run the following command:
5794

58-
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).
95+
```bash
96+
php artisan chat:session <session_uuid> -v
97+
```
5998

60-
## Security Vulnerabilities
99+
> Replace `<session_uuid>` with the actual session UUID.
100+
101+
5. Copy the provided command and run it in a new terminal tab. This command will show the AI response to your message.
102+
103+
![image](https://github.com/user-attachments/assets/1dfdfdd1-f69d-44af-afb2-807f9fa2da84)
104+
105+
## Available CLI Commands
106+
107+
The sample app provides several CLI commands for interacting with agents and managing the application:
108+
109+
- `php artisan agent:list`: List all available agents
110+
- `php artisan tool:list`: List all available tools
111+
- `php artisan chat`: Start a new chat session
112+
- `php artisan chat:session <session-id>`: Continue an existing chat session
113+
114+
Use the `-h` or `--help` option with any command to see more details about its usage.
115+
116+
## Available Agents
117+
118+
The sample app comes with several pre-configured agents, each designed for specific tasks:
119+
120+
### Site Status Checker
121+
122+
- **Key**: `site_status_checker`
123+
- **Description**: This agent specializes in checking the online status of websites. It can verify if a given URL is
124+
accessible, retrieve basic information about the site, and provide insights on potential issues if a site is
125+
offline.
126+
- **Capabilities**:
127+
- Check site availability
128+
- Retrieve DNS information
129+
- Perform ping tests
130+
- Provide troubleshooting steps for offline sites
131+
132+
### Order Assistant
133+
134+
- **Key**: `order_assistant`
135+
- **Description**: This agent helps customers with order-related questions. It can retrieve order information, check
136+
delivery status, and provide customer support for e-commerce related queries.
137+
- **Capabilities**:
138+
- Retrieve order numbers
139+
- Check delivery dates
140+
- Access customer profiles
141+
- Provide personalized assistance based on customer age and preferences
142+
143+
### Smart Home Control Assistant
144+
145+
- **Key**: `smart_home_control`
146+
- **Description**: This agent manages and controls various smart home devices across multiple rooms, including
147+
lights, thermostats, and TVs.
148+
- **Capabilities**:
149+
- List devices in specific rooms
150+
- Control individual devices (turn on/off, adjust settings)
151+
- Retrieve device status and details
152+
- Suggest energy-efficient settings
153+
154+
### Code Review Agent
155+
156+
- **Key**: `code_review`
157+
- **Description**: This agent specializes in reviewing code. It can analyze code files, provide feedback, and
158+
suggest improvements.
159+
- **Capabilities**:
160+
- List files in a project
161+
- Read file contents
162+
- Perform code reviews
163+
- Submit review comments
164+
165+
### Task Splitter
166+
167+
- **Key**: `task_splitter`
168+
- **Description**: This agent analyzes project descriptions and breaks them down into structured task lists with
169+
subtasks.
170+
- **Capabilities**:
171+
- Retrieve project descriptions
172+
- Create hierarchical task structures
173+
- Assign task priorities
174+
- Generate detailed subtasks
175+
176+
## Contributing
61177

62-
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [[email protected]](mailto:[email protected]). All security vulnerabilities will be promptly addressed.
178+
Contributions are welcome! Please feel free to submit a Pull Request.
63179

64180
## License
65181

66-
The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
182+
This sample app is open-source software licensed under the MIT license.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Agents\AgentsCaller;
6+
7+
use Spiral\JsonSchemaGenerator\Attribute\Field;
8+
9+
final class AskAgentInput
10+
{
11+
public function __construct(
12+
#[Field(title: 'Agent Name', description: 'The name of the agent to ask.')]
13+
public string $name,
14+
#[Field(title: 'Question', description: 'The question to ask the agent.')]
15+
public string $question,
16+
#[Field(title: 'Output Schema', description: 'The schema of the output.')]
17+
public string $outputSchema,
18+
) {}
19+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Agents\AgentsCaller;
6+
7+
use App\Agents\PhpTool;
8+
use LLM\Agents\Agent\AgentExecutor;
9+
use LLM\Agents\LLM\Prompt\Chat\ToolCallResultMessage;
10+
use LLM\Agents\LLM\Response\ToolCalledResponse;
11+
use LLM\Agents\Tool\ToolExecutor;
12+
13+
/**
14+
* @extends PhpTool<AskAgentInput>
15+
*/
16+
final class AskAgentTool extends PhpTool
17+
{
18+
public const NAME = 'ask_agent';
19+
20+
public function __construct(
21+
private readonly AgentExecutor $executor,
22+
private readonly ToolExecutor $toolExecutor,
23+
) {
24+
parent::__construct(
25+
name: self::NAME,
26+
inputSchema: AskAgentInput::class,
27+
description: 'Ask an agent with given name to execute a task.',
28+
);
29+
}
30+
31+
public function execute(object $input): string|\Stringable
32+
{
33+
$prompt = \sprintf(
34+
<<<'PROMPT'
35+
%s
36+
Important rules:
37+
- Think before responding to the user.
38+
- Don not markup the content. Only JSON is allowed.
39+
- Don't write anything except the answer using JSON schema.
40+
- Answer in JSON using this schema:
41+
%s
42+
PROMPT
43+
,
44+
$input->question,
45+
$input->outputSchema,
46+
);
47+
48+
// TODO: make async
49+
while (true) {
50+
$execution = $this->executor->execute($input->name, $prompt);
51+
$result = $execution->result;
52+
$prompt = $execution->prompt;
53+
54+
if ($result instanceof ToolCalledResponse) {
55+
foreach ($result->tools as $tool) {
56+
$functionResult = $this->toolExecutor->execute($tool->name, $tool->arguments);
57+
58+
$prompt = $prompt->withAddedMessage(
59+
new ToolCallResultMessage(
60+
id: $tool->id,
61+
content: [$functionResult],
62+
),
63+
);
64+
}
65+
66+
continue;
67+
}
68+
69+
break;
70+
}
71+
72+
return \json_encode($result->content);
73+
}
74+
}

0 commit comments

Comments
 (0)