Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extra function to return function return value from chat #120

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ It is compatible with Symfony and Laravel.

We are working to expand the support of different LLMs. Right now, we are supporting [OpenAI](https://openai.com/blog/openai-api) and [Ollama](https://ollama.ai/) that can be used to run LLM locally such as [Llama 2](https://llama.meta.com/).

If you want to use other LLMs, you can use [genossGPT](https://github.com/OpenGenenerativeAI/GenossGPT)
as a proxy.

We want to thank few amazing projects that we use here or inspired us:
- the learnings from using [LangChain](https://www.langchain.com/) and [LLamaIndex](https://www.llamaindex.ai/)
- the excellent work from the [OpenAI PHP SDK](https://github.com/openai-php/client).
Expand Down
79 changes: 79 additions & 0 deletions docusaurus/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,85 @@ $messages = [

$response = $chat->generateChat($messages);
```
## Get the token usage

When using OpenAI is important to know how many token are you using
since the [model pricing](https://openai.com/api/pricing/) is token based.

You can retrieve the total token usage using the `OpenAIChat::getTotalTokens()`
function, as follows:
```php
$chat = new OpenAIChat();

$answer = $chat->generateText('what is one + one ?');
printf("%s\n", $answer); # One plus one equals two
printf("Total tokens usage: %d\n", $chat->getTotalTokens()); # 19

$answer = $chat->generateText('And what is two + two ?');
printf("%s\n", $answer); # Two plus two equals four
printf("Total tokens usage: %d\n", $chat->getTotalTokens()); # 39
```

The `getTotalTokens()` is an incremental value that is increased on each
API call. For instance, in the previous example the first `what is one + one ?`
generated 19 tokens and the second call 20 tokens. The total number of
tokens is than 19 + 20 = 39 tokens.

## Get the last response from OpenAI

If you want to inspect the last API response from OpenAI you can use the function
`OpenAIChat::getLastReponse()` function.

This function returns an [OpenAI\Responses\Chat\CreateResponse](https://github.com/openai-php/client/blob/main/src/Responses/Chat/CreateResponse.php)
that contains the following properties:

```php
namespace OpenAI\Responses\Chat;

class CreateResponse
{
public readonly string $id;
public readonly string $object;
public readonly int $created;
public readonly string $model;
public readonly ?string $systemFingerprint;
public readonly array $choices;
public readonly CreateResponseUsage $usage;
}
```

The `usage` property is an object of the following [CreateResponseUsage](https://github.com/openai-php/client/blob/main/src/Responses/Chat/CreateResponseUsage.php)
class:

```php
namespace OpenAI\Responses\Chat;

final class CreateResponseUsage
{
public readonly int $promptTokens;
public readonly ?int $completionTokens;
public readonly int $totalTokens;
}
```

For instance, if you want to have specific information for the token usage,
you can then access the `usage` properties and then the sub-property, as follows:

```php
$chat = new OpenAIChat();

$answer = $chat->generateText('what is the capital of Italy ?');
$response = $chat->getLastResponse();

printf("Prompt tokens: %d\n", $response->usage->promptTokens);
printf("Completion tokens: %d\n", $response->usage->completionTokens);
printf("Total tokens: %d\n", $response->usage->totalTokens);
```

The value of the last `printf` is the total usage of the last response
(promptTokens + completionTokens) and should not be confused with the
`$chat->getTotalTokens()` function that is the sum of the previous totalTokens calls,
including the last one `what is the capital of Italy ?`.

## Tools

Expand Down
3 changes: 3 additions & 0 deletions src/Chat/ChatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public function generateStreamOfText(string $prompt): StreamInterface;
/** @param Message[] $messages */
public function generateChat(array $messages): string;

/** @param Message[] $messages */
public function generateChatOrReturnFunctionCalled(array $messages): string|FunctionInfo;

/** @param Message[] $messages */
public function generateChatStream(array $messages): StreamInterface;

Expand Down
2 changes: 2 additions & 0 deletions src/Chat/Enums/OpenAIChatModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ enum OpenAIChatModel
case Gpt35Turbo;
case Gpt4;
case Gpt4Turbo;
case Gpt4Omni;

public function getModelName(): string
{
return match ($this) {
OpenAIChatModel::Gpt35Turbo => 'gpt-3.5-turbo',
OpenAIChatModel::Gpt4 => 'gpt-4',
OpenAIChatModel::Gpt4Turbo => 'gpt-4-1106-preview',
OpenAIChatModel::Gpt4Omni => 'gpt-4o',
};
}
}
20 changes: 19 additions & 1 deletion src/Chat/OllamaChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,31 @@ public function generateText(string $prompt): string
}

/**
* Ollama does not support (yet) functions, this is an alias of generateText
* Ollama does not support (yet) functions, this is an alias of generateText and generateChat
*/
public function generateTextOrReturnFunctionCalled(string $prompt): string|FunctionInfo
{
return $this->generateText($prompt);
}

public function generateChatOrReturnFunctionCalled(array $messages): string|FunctionInfo
{
$params = [
...$this->modelOptions,
'model' => $this->config->model,
'messages' => $this->prepareMessages($messages),
'stream' => false,
];
$response = $this->sendRequest(
'POST',
'chat',
$params
);
$json = Utility::decodeJson($response->getBody()->getContents());

return $json['message']['content'];
}

public function generateStreamOfText(string $prompt): StreamInterface
{
$params = [
Expand Down
47 changes: 40 additions & 7 deletions src/Chat/OpenAIChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class OpenAIChat implements ChatInterface

public string $model;

private ?CreateResponse $lastResponse = null;

private int $totalTokens = 0;

/** @var array<string, mixed> */
private array $modelOptions = [];

Expand All @@ -37,11 +41,8 @@ class OpenAIChat implements ChatInterface

public ?FunctionInfo $requiredFunction = null;

public TokenUsage $usage;

public function __construct(?OpenAIConfig $config = null)
{
$this->usage = new TokenUsage();
if ($config instanceof OpenAIConfig && $config->client instanceof Client) {
$this->client = $config->client;
} else {
Expand All @@ -59,14 +60,27 @@ public function __construct(?OpenAIConfig $config = null)
public function generateText(string $prompt): string
{
$answer = $this->generate($prompt);

$this->handleTools($answer);

return $answer->choices[0]->message->content ?? '';
}

public function getLastResponse(): ?CreateResponse
{
return $this->lastResponse;
}

public function getTotalTokens(): int
{
return $this->totalTokens;
}

public function generateTextOrReturnFunctionCalled(string $prompt): string|FunctionInfo
{
$this->lastFunctionCalled = null;
$answer = $this->generate($prompt);

$toolsToCall = $this->getToolsToCall($answer);

foreach ($toolsToCall as $toolToCall) {
Expand Down Expand Up @@ -94,7 +108,26 @@ public function generateChat(array $messages): string
{
$openAiArgs = $this->getOpenAiArgs($messages);
$answer = $this->client->chat()->create($openAiArgs);
$this->usage->logLastUsage($answer);
$this->lastResponse = $answer;
$this->totalTokens += $answer->usage->totalTokens ?? 0;

return $answer->choices[0]->message->content ?? '';
}

public function generateChatOrReturnFunctionCalled(array $messages): string|FunctionInfo
{
$this->lastFunctionCalled = null;
$openAiArgs = $this->getOpenAiArgs($messages);
$answer = $this->client->chat()->create($openAiArgs);
$toolsToCall = $this->getToolsToCall($answer);

foreach ($toolsToCall as $toolToCall) {
$this->lastFunctionCalled = $toolToCall;
}

if ($this->lastFunctionCalled instanceof FunctionInfo) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @samuelgjekic,
I think it is great to add this possibility.

what if the previous answer was a function and now it is a text but the lastFunctionCalled is still the FunctionInfo from the previous call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm i did not think about this, my bad.. Anyways, does this not apply to the generateTextOrReturnFunction aswell? As they are the same.

I added a line that sets the lastFunction to null in the start of the method, is this good, or do you prefer that i check if $toolstocall is not null instead? If so dont merge yet and i will do it that way instead.

return $this->lastFunctionCalled;
}

return $answer->choices[0]->message->content ?? '';
}
Expand Down Expand Up @@ -159,10 +192,10 @@ private function generate(string $prompt): CreateResponse
$messages = $this->createOpenAIMessagesFromPrompt($prompt);
$openAiArgs = $this->getOpenAiArgs($messages);

$answer = $this->client->chat()->create($openAiArgs);
$this->usage->logLastUsage($answer);
$this->lastResponse = $this->client->chat()->create($openAiArgs);
$this->totalTokens += $this->lastResponse->usage->totalTokens ?? 0;

return $answer;
return $this->lastResponse;
}

/**
Expand Down
34 changes: 0 additions & 34 deletions src/Chat/TokenUsage.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,36 @@
namespace LLPhant\Embeddings\EmbeddingGenerator\Mistral;

use Exception;
use GuzzleHttp\Client;
use LLPhant\Embeddings\Document;
use LLPhant\Embeddings\EmbeddingGenerator\EmbeddingGeneratorInterface;
use LLPhant\Embeddings\EmbeddingGenerator\OpenAI\AbstractOpenAIEmbeddingGenerator;
use LLPhant\OpenAIConfig;
use OpenAI\Client;

use function getenv;
use function str_replace;

class MistralEmbeddingGenerator implements EmbeddingGeneratorInterface
class MistralEmbeddingGenerator extends AbstractOpenAIEmbeddingGenerator
{
public Client $client;

private readonly string $apiKey;

/**
* @throws Exception
*/
public function __construct(?OpenAIConfig $config = null)
{
$apiKey = $config->apiKey ?? getenv('MISTRAL_API_KEY');
if (! $apiKey) {
throw new Exception('You have to provide a MISTRAL_API_KEY env var to request Mistral .');
}
$this->apiKey = $apiKey;
$this->client = new Client();
}

/**
* Call out to OpenAI's embedding endpoint.
*
* @return float[]
*/
public function embedText(string $text): array
{
$text = str_replace("\n", ' ', $text);

$response = $this->client->post('https://api.mistral.ai/v1/embeddings', [
'body' => json_encode([
'model' => $this->getModelName(),
'input' => [$text],
], JSON_THROW_ON_ERROR),
'headers' => [
'Authorization' => 'Bearer '.$this->apiKey,
'Content-Type' => 'application/json',
],
]);

$searchResults = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
if (! is_array($searchResults)) {
throw new Exception("Request to Mistral didn't returned an array: ".$response->getBody()->getContents());
}
if ($config instanceof OpenAIConfig && $config->client instanceof Client) {
$this->client = $config->client;

if (! isset($searchResults['data'][0]['embedding'])) {
throw new Exception("Request to Mistral didn't returned expected format: ".$response->getBody()->getContents());
return;
}

return $searchResults['data'][0]['embedding'];
}

public function embedDocument(Document $document): Document
{
$text = $document->formattedContent ?? $document->content;
$document->embedding = $this->embedText($text);

return $document;
}

/**
* TODO: use the fact that we can send multiple texts to the embedding API
*
* @param Document[] $documents
* @return Document[]
*/
public function embedDocuments(array $documents): array
{
$embedDocuments = [];
foreach ($documents as $document) {
$embedDocuments[] = $this->embedDocument($document);
$apiKey = $config->apiKey ?? getenv('MISTRAL_API_KEY');
if (! $apiKey) {
throw new Exception('You have to provide a MISTRAL_API_KEY env var to request Mistral .');
}

return $embedDocuments;
$this->client = \OpenAI::factory()
->withApiKey($apiKey)
->withBaseUri('api.mistral.ai/v1')
->make();
}

public function getEmbeddingLength(): int
Expand Down