Skip to content

Commit

Permalink
Merge pull request #19 from GeraudBourdin/main
Browse files Browse the repository at this point in the history
Add tool Message type and update the function call example
  • Loading branch information
GeraudBourdin authored Jun 12, 2024
2 parents a85d6d2 + 1d7c29b commit a4600b8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 14 deletions.
63 changes: 58 additions & 5 deletions examples/function_calling.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Partitech\PhpMistral\Tools\Parameter;
use Partitech\PhpMistral\Tools\Tool;

$model = 'mistral-small-latest';
$model = 'mistral-large-latest';

// export MISTRAL_API_KEY=your_api_key
// export MISTRAL_API_KEY=
$apiKey = getenv('MISTRAL_API_KEY');
$client = new MistralClient(apiKey: $apiKey);

Expand Down Expand Up @@ -207,15 +207,68 @@
exit(1);
}

$toolCall = $chatResponse->getToolCalls();
$functionName = $toolCall[0]['function']['name'];
$functionParams = $toolCall[0]['function']['arguments'];
$toolCallResponse = $chatResponse->getToolCalls();
$toolCall = $toolCallResponse[0];
$functionName = $toolCall['function']['name'];
$functionParams = $toolCall['function']['arguments'];

// Call the proper function
$functionResult = $namesToFunctions[$functionName]($functionParams);

print_r($toolCall);
//Array
//(
// [id] => 1b9Ds90lR
// [function] => Array
// (
// [name] => retrievePaymentStatus
// [arguments] => Array
// (
// [transactionId] => T1001
// )
//
// )
//
//)

print_r($functionResult);
// Array
// (
// [status] => Paid
// )

print_r($functionParams);
//Array
//(
// [transactionId] => T1001
//)

// Add the last assistant message to messages history
$messages->addAssistantMessage(
content: $chatResponse->getMessage(),
toolCalls: $chatResponse->getToolCalls()
);

// Add the tool message to query mistral for a message
$messages->addToolMessage(
name: $toolCall['function']['name'],
content: $namesToFunctions[$functionName]($functionParams),
toolCallId: $toolCall['id']
);

try {
$chatResponse = $client->chat(
messages: $messages,
params: [
'model' => $model,
'tools' => $tools,
'tool_choice' => MistralClient::TOOL_CHOICE_AUTO
]
);
} catch (MistralClientException $e) {
echo $e->getMessage();
exit(1);
}

print_r($chatResponse->getMessage());
// The status of your transaction with ID T1001 is 'Paid'. Is there anything else you need help with?
66 changes: 58 additions & 8 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
class Message
{
private ?string $role = null;
private ?string $content = null;
private null|string|array $content = null;
private ?string $chunk = null;
private ?array $toolCalls = null;
private ?string $toolCallId = null;
private ?string $name = null;

/**
* @return ?string
Expand All @@ -26,17 +28,17 @@ public function setRole(string $role): void
}

/**
* @return ?string
* @return array|string|null
*/
public function getContent(): ?string
public function getContent(): null|array|string
{
return $this->content;
}

/**
* @param string $content
* @param string|array $content
*/
public function setContent(string $content): void
public function setContent(string|array $content): void
{
$this->content = $content;
}
Expand Down Expand Up @@ -69,18 +71,37 @@ public function getChunk(): string

public function toArray(): array
{
return [
$payLoad = [
'role' => $this->getRole(),
'content' => $this->getContent()
];

if($this->getRole() === 'tool') {
$payLoad['content'] = json_encode($this->getContent());
$payLoad['name'] = $this->getName();
$payLoad['tool_call_id'] = $this->getToolCallId();
}

if ($this->getRole() === 'assistant' && !is_null($this->getToolCalls())){
$payLoad['tool_calls'] = $this->getToolCalls(true);
}

return $payLoad;
}

/**
* @param bool|null $payload
* @return array|null
*/
public function getToolCalls(): ?array
public function getToolCalls(?bool $payload = false): ?array
{
return $this->toolCalls;
$response = $this->toolCalls;
if($payload){
foreach($response as &$toolCall){
$toolCall['function']['arguments'] = json_encode($toolCall['function']['arguments']);
}
}
return $response;
}

/**
Expand All @@ -89,11 +110,40 @@ public function getToolCalls(): ?array
*/
public function setToolCalls(?array $toolCalls): Message
{
if(null === $toolCalls){
return $this;
}

foreach($toolCalls as &$toolCall) {
if(is_array($toolCall['function']['arguments'])){
continue;
}
$toolCall['function']['arguments'] = json_decode($toolCall['function']['arguments'], true);
}

$this->toolCalls = $toolCalls;
return $this;
}

public function setName(?string $name): Message
{
$this->name = $name;
return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setToolCallId(?string $toolCallId): Message
{
$this->toolCallId = $toolCallId;
return $this;
}

public function getToolCallId(): ?string
{
return $this->toolCallId;
}
}
14 changes: 13 additions & 1 deletion src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,23 @@ public function addUserMessage(string $content): self
return $this;
}

public function addAssistantMessage(string $content): self
public function addToolMessage(string $name, array $content, string $toolCallId): self
{
$message = new Message();
$message->setRole('tool');
$message->setContent($content);
$message->setName($name);
$message->setToolCallId($toolCallId);
$this->addMessage($message);
return $this;
}

public function addAssistantMessage(string $content, null|array $toolCalls = null): self
{
$message = new Message();
$message->setRole('assistant');
$message->setContent($content);
$message->setToolCalls($toolCalls);
$this->addMessage($message);
return $this;
}
Expand Down

0 comments on commit a4600b8

Please sign in to comment.