📑

UnityでClaude3を使う

2024/03/12に公開

はじめに

皆さんこんにちは。Kaibaです。今回は最近GPT-4よりも賢いと話題のClaude 3をUnityから使う方法を纏めていきます。
PythonやTypeScriptで使用したい方は公式からSDKが出ているようなのでそちらをお使いください。

APIキーを作成する

以下のサイトからアカウントを作成します。
https://www.anthropic.com/api
ダッシュボードにきたら"Get API Keys"を押し、"Create Key"からAPIキーを入手します。

UnityでAPIを叩くC#スクリプトを書く

ClaudeのAPI仕様は以下のページにまとめられています。これを参考にUnityからUnityWebRequestでAPIを呼び出していきます。
https://docs.anthropic.com/claude/reference/messages_post

以下がClaudeのAPIを呼び出す関数です。UniTaskを使用しています。


public async UniTask<string> CallClaudeAPI(string system, CLAUDEChatMessage[] message)
{
        string url = "https://api.anthropic.com/v1/messages";

        CLAUDEChatBody chatBody = new CLAUDEChatBody
        {
            system = system,
            model = "claude-3-opus-20240229",
            messages = message,
            max_tokens = max_tokens
        };
        string body = JsonUtility.ToJson(chatBody);
        byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(body);


        using UnityWebRequest uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
        uwr.uploadHandler = new UploadHandlerRaw(jsonToSend);
        uwr.downloadHandler = new DownloadHandlerBuffer();
        uwr.SetRequestHeader("x-api-key", "API KEY HERE!");
        uwr.SetRequestHeader("anthropic-version", "2023-06-01");
        uwr.SetRequestHeader("Content-Type", "application/json");

        await uwr.SendWebRequest().ToUniTask();
        var response = uwr.downloadHandler.text;
        Debug.Log(response);

        var responseJson = JsonUtility.FromJson<CLAUDEChatResponse>(response);
        var output = responseJson.content[0].text;
        return output;
    
}

リクエストとレスポンス用に以下のようにクラスを定義しています。CLAUDEChatBodyは必要に応じてコメントアウトを外してパラメータを追加してください。

using System;

[Serializable]
public class CLAUDEChatMessage
{
    public string role;
    public string content;
}

[Serializable]
public class CLAUDEChatBody
{
    public string model;
    public string system;
    public CLAUDEChatMessage[] messages;
    public int max_tokens;
    //public string[] stop_sequences;
    //public bool stream;
    //public float temperature;
    //public float top_p;
    //public int top_k;
}

[Serializable]
public class CLAUDEChatResponse
{
    public string id;
    public string type;
    public string role;
    public CLAUDEChatContent[] content;
    public string model;
    public string stop_reason;
    public string stop_sequence;
    public CLAUDEChatUsage usage;
}


[Serializable]
public class CLAUDEChatContent
{
    public string type;
    public string text;
}


[Serializable]
public class CLAUDEChatUsage
{
    public int input_tokens;
    public int output_tokens;
}

モデルについて

公開されているモデルは現在以下のmodel nameを使用して指定できます。Haikuは2024年3月11日現在公開されていないようです。
https://docs.anthropic.com/claude/docs/models-overview

Model Latest API model name
Claude 3 Opus claude-3-opus-20240229
Claude 3 Sonnet claude-3-sonnet-20240229
Claude 3 Haiku Coming soon

おわりに

GPT-4より賢いのかには諸説ありますが、最上位モデルのOpusは確かにGPT-4と比較しても遜色ないレベルの返答を行うなと思いました。GPT Plusに課金していますがこれを機にClaudeに乗り換えようかなと真剣に検討しています。また、ClaudeはGPTにおいて制限が課されているロールプレイングに関しても前向きのようなのでAIキャラクターの作成などをしたい場合はClaudeを試してみてはいかがでしょうか?
以上、ご覧いただきありがとうございました~!

Discussion