Skip to content

Commit

Permalink
need to fix the response types before I can finish this test
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed May 10, 2023
1 parent 465dd93 commit e2dc902
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 103 deletions.
29 changes: 29 additions & 0 deletions app/Http/Controllers/OutboundController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Controllers;

use App\Models\Outbound;
use App\Models\Project;
use Illuminate\Http\Request;

class OutboundController extends Controller
{
public function __invoke(Project $project, Outbound $outbound)
{
/** @TODO find route binding for this */
if($outbound->project_id !== $project->id) {
abort(404);
}

/**
* @TODO they all have to be the same response type
* either a DTO or string
* maybe I need the status too?
* some might put a job on a queue
* some might need to be waited for
* some might use pusher so a queue etc
*/
//$response = outbound run
return response("", 200);
}
}
40 changes: 40 additions & 0 deletions app/Models/Outbound.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Models;

use App\Exceptions\ResponseTypeMissingException;
use App\Outbound\OutboundEnum;
use App\ResponseType\BaseResponseType;
use App\ResponseType\ResponseDto;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -20,4 +23,41 @@ class Outbound extends Model
public function project() {
return $this->belongsTo(Project::class);
}

/**
* @throws ResponseTypeMissingException
*/
public function run(User $user, string $request): ResponseDto
{
try {

$message = new Message([
'role' => 'user',
'content' => $request,
'user_id' => $user->id,
'project_id' => $this->project_id,
]);

$dto = ResponseDto::from([
'message' => $message,
]);

$this->currentResponseDto = $dto;

foreach ($this->project->response_types as $response_type_model) {
$responseType = $response_type_model->type->label();
$responseTypeClass = app("App\ResponseType\Types\\".$responseType, [
'project' => $this->project,
'response_dto' => $dto,
]);
/** @var BaseResponseType $responseTypeClass */
$this->currentResponseDto = $responseTypeClass->handle($response_type_model);
}

return $this->currentResponseDto;
} catch (\Exception $e) {
logger($e);
throw new ResponseTypeMissingException();
}
}
}
35 changes: 0 additions & 35 deletions app/Models/ResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,5 @@ public function project()
return $this->belongsTo(Project::class);
}

/**
* @throws ResponseTypeMissingException
*/
public function run(User $user, string $request): ResponseDto
{
try {

$message = new Message([
'role' => 'user',
'content' => $request,
'user_id' => $user->id,
'project_id' => $this->project_id,
]);

$dto = ResponseDto::from([
'message' => $message,
]);

$this->currentResponseDto = $dto;

foreach ($this->project->response_types as $response_type_model) {
$responseType = $response_type_model->type->label();
$responseTypeClass = app("App\ResponseType\Types\\".$responseType, [
'project' => $this->project,
'response_dto' => $dto,
]);
/** @var BaseResponseType $responseTypeClass */
$this->currentResponseDto = $responseTypeClass->handle($response_type_model);
}

return $this->currentResponseDto;
} catch (\Exception $e) {
logger($e);
throw new ResponseTypeMissingException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function up(): void
$table->foreignIdFor(Project::class);
$table->timestamps();
});

}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Feature/Http/Controllers/OutboundControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

it("should take request and kick off things", function () {

});
83 changes: 83 additions & 0 deletions tests/Feature/Models/OutboundTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<?php


use App\LLMModels\OpenAi\EmbeddingsResponseDto;
use App\Models\Document;
use App\Models\Outbound;
use App\Models\Project;
use App\Models\ResponseType;
use App\Models\User;
use App\Outbound\OutboundEnum;
use App\ResponseType\ResponseDto;
use App\ResponseType\ResponseTypeEnum;
use Facades\App\LLMModels\OpenAi\ClientWrapper;

it("should have factory", function () {

Expand All @@ -17,3 +25,78 @@
expect($model->project->id)->not->toBeNull();
expect($model->project->outbounds->first()->id)->not->toBeNull();
});

it("should ru the related response types", function () {
$user = User::factory()->create();
$request = 'Foo bar';

$embeddings = get_fixture('embedding_response.json');

$project = Project::factory()->create();
$outbound = Outbound::factory()->create([
'project_id' => $project->id
]);

/** @var ResponseType $responseType */
ResponseType::factory()->create([
'type' => ResponseTypeEnum::EmbedQuestion,
]);

$dto = new EmbeddingsResponseDto(
data_get($embeddings, 'data.0.embedding'),
1000
);

ClientWrapper::shouldReceive('getEmbedding')
->once()
->andReturn($dto);

$this->assertDatabaseCount('messages', 0);
/** @var ResponseDto $results */
$outbound->run($user, $request);
$this->assertDatabaseCount('messages', 1);

});


//public function test_runs_embed_then_search()
//{
// $user = User::factory()->create();
// $request = 'History';
//
// $embeddings = get_fixture('embedding_response.json');
//
// Document::factory()->withEmbedData()->create();
//
// $project = Project::factory()->create();
//
// $responseType1 = ResponseType::factory()->create([
// 'type' => ResponseTypeEnum::EmbedQuestion,
// 'project_id' => $project->id,
// 'order' => 1,
// ]);
//
// $responseType2 = ResponseType::factory()->create([
// 'type' => ResponseTypeEnum::VectorSearch,
// 'project_id' => $project->id,
// 'order' => 2,
// ]);
//
// $responseType3 = ResponseType::factory()->create([
// 'type' => ResponseTypeEnum::CombineContent,
// 'project_id' => $project->id,
// 'order' => 4,
// ]);
//
// $dto = new EmbeddingsResponseDto(
// data_get($embeddings, 'data.0.embedding'),
// 1000
// );
//
// ClientWrapper::shouldReceive('getEmbedding')
// ->once()
// ->andReturn($dto);
// /** @var ResponseDto $results */
// $results = $responseType1->run($user, $request);
// $this->assertNotNull($results->response);
//}
68 changes: 0 additions & 68 deletions tests/Feature/Models/ResponseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,4 @@ public function test_factory_rt()
$this->assertNotNull($model->project->response_types->first()->id);
}

public function test_rt_run()
{
$user = User::factory()->create();
$request = 'Foo bar';

$embeddings = get_fixture('embedding_response.json');

/** @var ResponseType $responseType */
$responseType = ResponseType::factory()->create([
'type' => ResponseTypeEnum::EmbedQuestion,
]);

$dto = new EmbeddingsResponseDto(
data_get($embeddings, 'data.0.embedding'),
1000
);

ClientWrapper::shouldReceive('getEmbedding')
->once()
->andReturn($dto);

$this->assertDatabaseCount('messages', 0);
/** @var ResponseDto $results */
$responseType->run($user, $request);
$this->assertDatabaseCount('messages', 1);
}

public function test_runs_embed_then_search()
{
$user = User::factory()->create();
$request = 'History';

$embeddings = get_fixture('embedding_response.json');

Document::factory()->withEmbedData()->create();

$project = Project::factory()->create();

$responseType1 = ResponseType::factory()->create([
'type' => ResponseTypeEnum::EmbedQuestion,
'project_id' => $project->id,
'order' => 1,
]);

$responseType2 = ResponseType::factory()->create([
'type' => ResponseTypeEnum::VectorSearch,
'project_id' => $project->id,
'order' => 2,
]);

$responseType3 = ResponseType::factory()->create([
'type' => ResponseTypeEnum::CombineContent,
'project_id' => $project->id,
'order' => 4,
]);

$dto = new EmbeddingsResponseDto(
data_get($embeddings, 'data.0.embedding'),
1000
);

ClientWrapper::shouldReceive('getEmbedding')
->once()
->andReturn($dto);
/** @var ResponseDto $results */
$results = $responseType1->run($user, $request);
$this->assertNotNull($results->response);
}
}

0 comments on commit e2dc902

Please sign in to comment.