php流式调用claude api实现多轮对话示例代码

代码语言:php

所属分类:其他

代码描述:php流式调用claude api实现多轮对话示例代码

代码标签: php 流式 调用 claude api 多轮 对话 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
//申请key地址:https://console.anthropic.com/account/keys
$ch = curl_init('https://api.anthropic.com/v1/messages');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'X-API-Key: YOUR_API_KEY',
    'anthropic-version: 2023-06-01'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'model' => 'claude-3-opus-20240229',
    'messages' => [['role' => 'user', 'content' => 'Hello']],
    'max_tokens' => 1024,
    'stream' => true
]));
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) {
    $events = explode("\n", trim($data));
    foreach ($e.........完整代码请登录后点击上方下载按钮下载查看

网友评论0