「WSL2上で動いてるDify」と「Windows上のPython」とをAPI連携してみる

Pythonスクリプト

import requests
import json

def post_request(api_key):
    
    url = "http://localhost/v1/chat-messages"

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }

    data = json.dumps({
        "inputs": {},
        "query": "What are the specs of the iPhone 13 Pro Max?",
        "response_mode": "streaming",
        "conversation_id": "",
        "user": "abc-123",
    })

    response = requests.post(url, headers=headers, data=data)

    #>>> type(result)
    #<class 'requests.models.Response'>

    answer_text = ""
    if response.status_code == 200:
        for line in response.iter_lines():
            if line != b"":
                try:
                    decoded_line = json.loads(line.decode("utf-8").lstrip("data: "))
                    match decoded_line:
                        case {"answer": answer}:
                            answer_text += answer
                except:
                    pass

    return answer_text

if __name__ == "__main__":
    api_key = "app-33s7mepWNIp40AjK7O7S7zHc"
    result = post_request(api_key=api_key)
    print(result)

APIキーはDifyで作成できます。

結果

質問はスクリプト内に記載されている「What are the specs of the iPhone 13 Pro Max?」です。
上記スクリプトを実行すると以下の結果が返ってきました。
Dify内のPhi-3が答えてくれています。

The iPhone 13 Pro Max is Apple's flagship smartphone and features the following specifications:


- **Display**: Super Retina XDR OLED display, with a 6.7 inch (diagonal) True Tone display that covers nearly the entire screen surface; it has a resolution of 2778 x 1284 pixels and supports HDR10.

- **Processor**: A custom-designed A15 Bionic chip, featuring a 6-core CPU (1x Firestorm & 5x Icestorm), an Apple-designed GPU, and support for 5G networks.

- **Memory**: It comes with either 6 GB or 8 GB of LPDDR4X RAM.

- **Storage Options**: Available in storage options ranging from 128GB to 1TB (expandable via Lightning connector).

- **Camera System**: Triple camera system with a 12 MP Ultra Wide, 12 MP Telephoto, and 12 MP Wide lenses; ProRAW and ProVideo video recording capabilities.

- **Battery Life**: A rechargeable non-removable Li-Po (Lithium Polymer) battery with a capacity of 3687 mAh, supporting Fast Charging, MagSafe wireless charging, and support for reverse wireless data transfer.

- **Connectivity**: Wireless connectivity includes Wi-Fi 6E (IEEE 802.11ax), Bluetooth 5.2, and dual-band cellular capabilities supporting all major carriers worldwide.

- **Operating System**: Runs on iOS 15 or a later version if updated at launch.

- **Dimensions & Weight**: Measures 6.7 inches in height x 3.0 inches in width x 12.8 inches in depth, and weighs approximately 0.69 pounds (322 grams).

「**(文字)**」はMarkdown記法のボールド(太字で強調)だと思います。



このエントリーをはてなブックマークに追加