File: responses_input_tokens.py

package info (click to toggle)
python-openai 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,528 kB
  • sloc: python: 68,384; sh: 161; makefile: 7
file content (54 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import List

from openai import OpenAI
from openai.types.responses.tool_param import ToolParam
from openai.types.responses.response_input_item_param import ResponseInputItemParam


def main() -> None:
    client = OpenAI()
    tools: List[ToolParam] = [
        {
            "type": "function",
            "name": "get_current_weather",
            "description": "Get current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City and state, e.g. San Francisco, CA",
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["c", "f"],
                        "description": "Temperature unit to use",
                    },
                },
                "required": ["location", "unit"],
                "additionalProperties": False,
            },
            "strict": True,
        }
    ]

    input_items: List[ResponseInputItemParam] = [
        {
            "type": "message",
            "role": "user",
            "content": [{"type": "input_text", "text": "What's the weather in San Francisco today?"}],
        }
    ]

    response = client.responses.input_tokens.count(
        model="gpt-5",
        instructions="You are a concise assistant.",
        input=input_items,
        tools=tools,
        tool_choice={"type": "function", "name": "get_current_weather"},
    )
    print(f"input tokens: {response.input_tokens}")


if __name__ == "__main__":
    main()