Skip to content

Commit 5319422

Browse files
committed
Http client refactoring
1 parent 963c8e1 commit 5319422

31 files changed

+710
-505
lines changed
Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,89 @@
1-
# Work in progress
1+
---
2+
title: 'Quickstart'
3+
description: 'Start working with LLMs in under 5 minutes'
4+
---
5+
6+
This guide will help you get started with Polyglot in your PHP project in under 5 minutes.
7+
8+
For detailed setup instructions, see [Setup](setup).
9+
10+
11+
## Install Polyglot with Composer
12+
13+
Run following command in your terminal:
14+
15+
```bash
16+
composer require cognesy/instructor-polyglot
17+
```
18+
19+
20+
## Create and Run Example
21+
22+
### Step 1: Prepare your OpenAI API Key
23+
24+
In this example, we'll use OpenAI as the LLM provider. You can get it from the [OpenAI dashboard](https://platform.openai.com/).
25+
26+
### Step 2: Create a New PHP File
27+
28+
In your project directory, create a new PHP file `test-instructor.php`:
29+
30+
```php
31+
<?php
32+
require __DIR__ . '/vendor/autoload.php';
33+
34+
use Cognesy\Instructor\Instructor;
35+
36+
// Set up OpenAI API key
37+
$apiKey = 'your-openai-api-key';
38+
putenv("OPENAI_API_KEY=" . $apiKey);
39+
// WARNING: In real project you should set up API key in .env file.
40+
41+
// Step 1: Define target data structure(s)
42+
class City {
43+
public string $name;
44+
public string $country;
45+
public int $population;
46+
}
47+
48+
// Step 2: Use Instructor to run LLM inference
49+
$city = (new Instructor)->withConnection('openai')->respond(
50+
messages: 'What is the capital of France?',
51+
responseModel: City::class,
52+
);
53+
54+
var_dump($city);
55+
```
56+
57+
<Warning>
58+
You should never put your API keys directly in your real project code to avoid getting them compromised. Set them up in your .env file.
59+
</Warning>
60+
61+
### Step 3: Run the Example
62+
63+
Now, you can run the example:
64+
65+
```bash
66+
php test-instructor.php
67+
68+
# Output:
69+
# object(City)#1 (3) {
70+
# ["name"]=>
71+
# string(5) "Paris"
72+
# ["country"]=>
73+
# string(6) "France"
74+
# ["population"]=>
75+
# int(2148000)
76+
# }
77+
```
78+
79+
80+
## Next Steps
81+
82+
You can start using Instructor in your project right away after installation.
83+
84+
But it's recommended to publish configuration files and prompt templates to your project directory, so you can
85+
customize the library's behavior and use your own prompt templates.
86+
87+
You should also set up LLM provider API keys in your `.env` file instead of putting them directly in your code.
88+
89+
See [Setup Instructions](setup) for more details.

examples/A02_Advanced/CustomClientParameters/run.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class User {
3434
model: 'deepseek-chat',
3535
maxTokens: 128,
3636
httpClient: 'guzzle',
37-
providerType: LLMProviderType::OpenAICompatible,
37+
providerType: LLMProviderType::OpenAICompatible->value,
3838
);
3939

4040
// Get Instructor with the default client component overridden with your own

examples/C02_LLMAdvanced/CustomClientParameters/run.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
model: 'deepseek-chat',
2929
maxTokens: 128,
3030
httpClient: 'guzzle',
31-
providerType: LLMProviderType::OpenAICompatible,
31+
providerType: LLMProviderType::OpenAICompatible->value,
3232
);
3333

3434
$answer = (new Inference)

src-polyglot/Embeddings/Data/EmbeddingsConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717
public int $maxInputs = 0,
1818
public array $metadata = [],
1919
public string $httpClient = '',
20-
public ?LLMProviderType $providerType = null,
20+
public string $providerType = LLMProviderType::OpenAI->value,
2121
) {}
2222

2323
public static function load(string $connection) : EmbeddingsConfig {
@@ -33,7 +33,7 @@ public static function load(string $connection) : EmbeddingsConfig {
3333
maxInputs: Settings::get('embed', "connections.$connection.maxInputs", 1),
3434
metadata: Settings::get('embed', "connections.$connection.metadata", []),
3535
httpClient: Settings::get('embed', "connections.$connection.httpClient", ''),
36-
providerType: LLMProviderType::from(Settings::get('embed', "connections.$connection.providerType")),
36+
providerType: Settings::get('embed', "connections.$connection.providerType", LLMProviderType::OpenAI->value),
3737
);
3838
}
3939
}

src-polyglot/Embeddings/Embeddings.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public function create(string|array $input, array $options = []) : EmbeddingsRes
8585

8686
protected function getDriver(EmbeddingsConfig $config, CanHandleHttp $httpClient) : CanVectorize {
8787
return match ($config->providerType) {
88-
LLMProviderType::Azure => new AzureOpenAIDriver($config, $httpClient, $this->events),
89-
LLMProviderType::CohereV1 => new CohereDriver($config, $httpClient, $this->events),
90-
LLMProviderType::Gemini => new GeminiDriver($config, $httpClient, $this->events),
91-
LLMProviderType::Mistral => new OpenAIDriver($config, $httpClient, $this->events),
92-
LLMProviderType::OpenAI => new OpenAIDriver($config, $httpClient, $this->events),
93-
LLMProviderType::Ollama => new OpenAIDriver($config, $httpClient, $this->events),
94-
LLMProviderType::Jina => new JinaDriver($config, $httpClient, $this->events),
95-
default => throw new InvalidArgumentException("Unknown client: {$config->providerType->value}"),
88+
LLMProviderType::Azure->value => new AzureOpenAIDriver($config, $httpClient, $this->events),
89+
LLMProviderType::CohereV1->value => new CohereDriver($config, $httpClient, $this->events),
90+
LLMProviderType::Gemini->value => new GeminiDriver($config, $httpClient, $this->events),
91+
LLMProviderType::Mistral->value => new OpenAIDriver($config, $httpClient, $this->events),
92+
LLMProviderType::OpenAI->value => new OpenAIDriver($config, $httpClient, $this->events),
93+
LLMProviderType::Ollama->value => new OpenAIDriver($config, $httpClient, $this->events),
94+
LLMProviderType::Jina->value => new JinaDriver($config, $httpClient, $this->events),
95+
default => throw new InvalidArgumentException("Unknown client: {$config->providerType}"),
9696
};
9797
}
9898
}

src-polyglot/Http/Adapters/LaravelResponseAdapter.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Cognesy\Polyglot\Http\Adapters;
44

5-
use Cognesy\Polyglot\Http\Contracts\ResponseAdapter;
5+
use Cognesy\Polyglot\Http\Contracts\HttpClientResponse;
66
use Generator;
77
use Illuminate\Http\Client\Response;
88

9-
class LaravelResponseAdapter implements ResponseAdapter
9+
class LaravelResponseAdapter implements HttpClientResponse
1010
{
1111
public function __construct(
1212
private Response $response,
@@ -40,20 +40,4 @@ public function streamContents(int $chunkSize = 1): Generator
4040
yield $stream->read($chunkSize);
4141
}
4242
}
43-
44-
// public function streamContents(int $chunkSize = 1): Generator
45-
// {
46-
// if (!$this->streaming) {
47-
// yield $this->getContents();
48-
// return;
49-
// }
50-
//
51-
// $resource = StreamWrapper::getResource($this->response->toPsrResponse()->getBody());
52-
//
53-
// while (!feof($resource)) {
54-
// yield fread($resource, $chunkSize);
55-
// }
56-
//
57-
// fclose($resource);
58-
// }
5943
}

src-polyglot/Http/Adapters/PsrResponseAdapter.php

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

33
namespace Cognesy\Polyglot\Http\Adapters;
44

5-
use Cognesy\Polyglot\Http\Contracts\ResponseAdapter;
5+
use Cognesy\Polyglot\Http\Contracts\HttpClientResponse;
66
use Generator;
77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\StreamInterface;
99

10-
class PsrResponseAdapter implements ResponseAdapter
10+
class PsrResponseAdapter implements HttpClientResponse
1111
{
1212
private ResponseInterface $response;
1313
private StreamInterface $stream;

src-polyglot/Http/Adapters/SymfonyResponseAdapter.php

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

33
namespace Cognesy\Polyglot\Http\Adapters;
44

5-
use Cognesy\Polyglot\Http\Contracts\ResponseAdapter;
5+
use Cognesy\Polyglot\Http\Contracts\HttpClientResponse;
66
use Generator;
77
use Symfony\Contracts\HttpClient\HttpClientInterface;
88
use Symfony\Contracts\HttpClient\ResponseInterface;
99

10-
class SymfonyResponseAdapter implements ResponseAdapter
10+
class SymfonyResponseAdapter implements HttpClientResponse
1111
{
1212
private ResponseInterface $response;
1313
private HttpClientInterface $client;

src-polyglot/Http/Contracts/CanHandleHttp.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55

66
interface CanHandleHttp
77
{
8-
public function handle(HttpClientRequest $request) : ResponseAdapter;
9-
public function pool(array $requests, ?int $maxConcurrent = null): array;
8+
public function handle(HttpClientRequest $request) : HttpClientResponse;
109
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Cognesy\Polyglot\Http\Contracts;
4+
5+
interface CanHandleRequestPool
6+
{
7+
public function pool(array $requests, ?int $maxConcurrent = null): array;
8+
}

0 commit comments

Comments
 (0)