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 3 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: 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
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
17 changes: 17 additions & 0 deletions src/Chat/OpenAIChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ public function generateChat(array $messages): string
return $answer->choices[0]->message->content ?? '';
}

public function generateChatOrReturnFunctionCalled(array $messages): string|FunctionInfo
{
$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 ?? '';
}

/**
* @param Message[] $messages
*/
Expand Down