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

代码语言:php

所属分类:其他

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

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

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

<?php
//申请key地址:https://console.anthropic.com/account/keys
$ANTHROPIC_API_KEY = 'your_api_key';  // Replace with your actual API key

$url = 'https://api.anthropic.com/v1/messages';

$data = array(
    "model" => "claude-3-5-sonnet-20240620",
    "max_tokens" => 1024,
    "messages" => array(
        array("role" => "user", "content" => "Hello, Claude"),
         array("role" => "assistant", "content" => "Hi"),
         array("role" => "user", "content" => "who are you?")
        
    )
);

$data_string = json_encode($data);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_.........完整代码请登录后点击上方下载按钮下载查看

网友评论0