{"repo": "answerdotai/fastllm", "files": {"nbs/03_oai_chat.ipynb": {"79b1aae2c06f65bc5c16293705373b23dc6e4f5c": {"t": 1786068254.8688993, "n": 157305, "h": "b7320180383e035a", "c": "{\n \"cells\": [\n {\n \"cell_type\": \"markdown\",\n \"id\": \"ea67cfc1\",\n \"metadata\": {},\n \"source\": [\n \"# OpenAI Chat Completions API\\n\",\n \"\\n\",\n \"OpenAI Chat Completions API from/to translation helpers\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"c12ff842\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| default_exp openai_chat\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"7eeeee1e\",\n \"metadata\": {},\n \"source\": [\n \"## Imports\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"493e3606\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"import json\\n\",\n \"from collections import Counter\\n\",\n \"from fastcore.utils import *\\n\",\n \"from fastcore.meta import *\\n\",\n \"from fastspec.errors import api_error_from_event\\n\",\n \"\\n\",\n \"from fastllm.types import *\\n\",\n \"from aidialog.msg_parts import (Part, PartType, Msg, Text, Thinking, Refusal, ToolUse, ToolResult, tool_text,\\n\",\n \" InputImage, InputAudio, InputVideo, InputFile, sys_text, part_txt, data_url)\\n\",\n \"from fastllm.streaming import *\\n\",\n \"from fastllm.streaming import mk_acollect_stream\\n\",\n \"from fastllm.openai_responses import norm_usage\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"eb01368b\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| hide\\n\",\n \"import yaml,base64,httpx\\n\",\n \"from fastspec.spec import *\\n\",\n \"from fastspec.oapi import *\\n\",\n \"from fastcore.test import *\\n\",\n \"from fastcore.funccall import get_schema\\n\",\n \"from cachy.core import enable_cachy, disable_cachy\\n\",\n \"from importlib.resources import files\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"453393ec\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"enable_cachy(hdrs=('content-type',))\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"ed7daa93\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"SpecParser(base_url='https://api.openai.com/v1', ops=241)\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"specs_path = files('fastllm') / 'specs'\\n\",\n \"oai_spec = SpecParser.from_openapi(dict2obj(yaml.safe_load((specs_path/'openai.with-code-samples.yml').read_text())))\\n\",\n \"oai_spec\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"b2428d8d\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"oai_cli = OpenAPIClient(oai_spec, headers={\\\"Authorization\\\": f\\\"Bearer {os.environ['OPENAI_API_KEY']}\\\"})\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"7ff3ba5b\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"dict_keys(['assistants', 'audio', 'batches', 'chat', 'completions', 'containers', 'conversations', 'embeddings', 'evals', 'files', 'fine_tuning', 'images', 'models', 'moderations', 'organization', 'projects', 'realtime', 'responses', 'threads', 'uploads', 'vector_stores', 'videos', 'skills', 'chatkit'])\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"oai_cli.groups.keys()\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"826a2d15\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"kimi_cli = OpenAPIClient(oai_spec, headers={\\\"Authorization\\\": f\\\"Bearer {os.environ['MOONSHOT_API_KEY']}\\\"})\\n\",\n \"for op in kimi_cli.ops: op.base_url = 'https://api.moonshot.ai/v1'\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"739fd939\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"qwen_cli = OpenAPIClient(oai_spec, headers={\\\"Authorization\\\": f\\\"Bearer {os.environ['FIREWORKS_API_KEY']}\\\"})\\n\",\n \"for op in qwen_cli.ops: op.base_url = 'https://api.fireworks.ai/inference/v1'\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"483750f0\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"ds_cli = OpenAPIClient(oai_spec, headers={\\\"Authorization\\\": f\\\"Bearer {os.environ['DEEPSEEK_API_KEY']}\\\"})\\n\",\n \"for op in ds_cli.ops: op.base_url = 'https://api.deepseek.com/v1'\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"0dfdca75\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"# qwen_cli = OpenAPIClient(oai_spec, headers={\\\"Authorization\\\": f\\\"Bearer {os.environ['QWEN_API_KEY']}\\\"})\\n\",\n \"# for op in qwen_cli.ops: op.base_url = 'https://dashscope.aliyuncs.com/compatible-mode/v1'\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"77de8c43\",\n \"metadata\": {},\n \"source\": [\n \"## Normalize\\n\",\n \"\\n\",\n \"Helpers to transform API responses into the internal representation types\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"fdc79537\",\n \"metadata\": {},\n \"source\": [\n \"### Tool Calls\\n\",\n \"\\n\",\n \"Normalize OpenAI tool calls shape(s)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"96289ce5\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def lite_mk_func(f):\\n\",\n \" if isinstance(f, dict): return f\\n\",\n \" return {'type':'function', 'function':get_schema(f, pname='parameters')}\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"70bd0555\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def simple_add(a:int,b:int):\\n\",\n \" 'add numbers'\\n\",\n \" return a + b\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"57222953\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"{'type': 'function',\\n\",\n \" 'function': {'name': 'simple_add',\\n\",\n \" 'description': 'add numbers',\\n\",\n \" 'parameters': {'type': 'object',\\n\",\n \" 'properties': {'a': {'description': '', 'type': 'integer'},\\n\",\n \" 'b': {'description': '', 'type': 'integer'}},\\n\",\n \" 'required': ['a', 'b']}}}\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"sch = lite_mk_func(simple_add);sch\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"b4f0e4d0\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def norm_tool_calls(resp, delta=False):\\n\",\n \" \\\"Extract Chat Completions tool calls as normalized tool calls, optionally from streaming delta events\\\"\\n\",\n \" out = []\\n\",\n \" key = 'delta' if delta else 'message'\\n\",\n \" if not (tcs:= nested_idx(resp, 'choices', 0, key, 'tool_calls')): return out\\n\",\n \" for tc in tcs:\\n\",\n \" if not (fn:=tc.get(\\\"function\\\")): continue\\n\",\n \" raw = {k:v for k,v in tc.items() if k not in (\\\"id\\\",\\\"function\\\")}\\n\",\n \" args = json.loads(fn.get(\\\"arguments\\\")) if not delta else {'_delta': fn.get(\\\"arguments\\\")}\\n\",\n \" out.append(ToolUse(id=tc.get(\\\"id\\\",\\\"\\\"), name=fn.get(\\\"name\\\",\\\"\\\"), arguments=args, raw=raw))\\n\",\n \" return out\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"cf75935f\",\n \"metadata\": {},\n \"source\": [\n \"User defined:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"60d1d71a\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"[ToolUse(raw={'type': 'function'}, cache_control=None, id='call_s8RcV6qS8cVwid4zxN2w28wg', name='simple_add', arguments={'a': 3, 'b': 5}, server=False, text=None),\\n\",\n \" ToolUse(raw={'type': 'function'}, cache_control=None, id='call_9X1InU3SdJxE80GBHpEbUPVk', name='simple_add', arguments={'a': 10, 'b': 20}, server=False, text=None)]\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model='gpt-4o-mini', messages=[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"What is 3 + 5 and 10 + 20? Use the simple_add tool in parallel.\\\"}], tools=[sch])\\n\",\n \"norm_tool_calls(resp)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"4af4a420\",\n \"metadata\": {},\n \"source\": [\n \"### Usage\\n\",\n \"\\n\",\n \"Same as responses api\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"1e960c79\",\n \"metadata\": {},\n \"source\": [\n \"Text response:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"18a2eb5d\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"fastllm.types.Usage(prompt_tokens=46, completion_tokens=10, total_tokens=56, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 46, 'completion_tokens': 10, 'total_tokens': 56, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model='gpt-4o-mini', messages=[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"Hi!\\\"}], tools=[sch])\\n\",\n \"norm_usage(resp)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"bca230e1\",\n \"metadata\": {},\n \"source\": [\n \"Tool call response:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"9532b617\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"fastllm.types.Usage(prompt_tokens=66, completion_tokens=52, total_tokens=118, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model='gpt-4o-mini', messages=[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"What is 3 + 5 and 10 + 20? Use the simple_add tool in parallel.\\\"}], tools=[sch])\\n\",\n \"norm_usage(resp)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"d587ea24\",\n \"metadata\": {},\n \"source\": [\n \"### Finish Reason\\n\",\n \"\\n\",\n \"Normalize OpenAI finish reason shape(s)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"b387d62d\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def norm_finish(resp, tcs=None):\\n\",\n \" \\\"Canonicalize finish_reason to OpenAI Chat values: stop, tool_calls, length, content_filter.\\\"\\n\",\n \" r = nested_idx(resp, 'choices', 0, 'finish_reason')\\n\",\n \" return FinishReason.tool_calls if r==FinishReason.stop and any(~L(tcs).attrgot('server')) else r \"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"7ed67355\",\n \"metadata\": {},\n \"source\": [\n \"### Non-stream Completion\\n\",\n \"\\n\",\n \"Normalize OpenAI Responses API object into Completion.\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"93ba32f4\",\n \"metadata\": {},\n \"source\": [\n \"**NB:** Per the OpenAI spec, `text`, `refusal`, and `summary_text` fields are all **required** — the `.get(k, \\\"\\\")` fallbacks are purely defensive against malformed responses. `OutputMessageContent` is a discriminated `oneOf` with exactly two variants: `OutputTextContent` (`output_text`) and `RefusalContent` (`refusal`) — no other content types exist in message blocks.\\n\",\n \"\\n\",\n \"`summary` is an array of `SummaryTextContent` only — no discriminated union, just one type. So the `if s.get(\\\"type\\\") == \\\"summary_text\\\"` check is technically redundant (it'll always be `summary_text`), and `text` is required.\\n\",\n \"\\n\",\n \"Additionally, `Response.output`, `OutputMessage.content`, and `ReasoningItem.summary` are all **required** fields — so `.get()` with fallback defaults is defensive but not strictly necessary.\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"5abf90bb\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def norm_parts(resp):\\n\",\n \" \\\"Normalize chat.completions response object into Completion.\\\"\\n\",\n \" msg = nested_idx(resp, 'choices', 0, 'message') or {}\\n\",\n \" parts = []\\n\",\n \" if thinking := msg.get('reasoning_content'): parts.append(Thinking(thinking))\\n\",\n \" if cts := msg.get('content'): parts.append(Text(cts, citations=msg.get('annotations',[])))\\n\",\n \" if ref := msg.get('refusal'): parts.append(Refusal(ref))\\n\",\n \" return parts + norm_tool_calls(resp)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"a8835547\",\n \"metadata\": {},\n \"source\": [\n \"Users can define `norm_tool_calls`, `norm_parts`, `norm_finish` and `norm_usage` for a given provider, and `mk_completion` will be used to create the canonical `Completion` object based on those functions:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"a861fd9a\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"norm_funcs = dict(norm_tool_calls=norm_tool_calls, norm_parts=norm_parts, norm_finish=norm_finish, norm_usage=norm_usage)\\n\",\n \"api_registry.register('openai_chat', **norm_funcs)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"14836606\",\n \"metadata\": {},\n \"source\": [\n \"#### Text\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"41632377\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"Hi! How can I assist you today?\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `gpt-4o-mini`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model='gpt-4o-mini', message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text='Hi! How can I assist you today?', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'id': 'chatcmpl-DpvqMSJEgqQQysCWo6tQhcHomxbEg', 'object': 'chat.completion', 'created': 1781269450, 'model': 'gpt-4o-mini-2024-07-18', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': 'Hi! How can I assist you today?', 'refusal': None, 'annotations': []}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'service_tier': 'default', 'system_fingerprint': 'fp_7ef2449383'})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"mn,api_name,vnd_nm = 'gpt-4o-mini', 'openai_chat', 'openai_chat'\\n\",\n \"resp = await oai_cli.chat.create_chat_completion(model=mn, messages=[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"Say hi\\\"}])\\n\",\n \"comp = mk_completion(resp, mn, api_name, vnd_nm); comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"ca2f6f2e\",\n \"metadata\": {},\n \"source\": [\n \"#### Tool call\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"5b4af632\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"\\n\",\n \"\\n\",\n \"🔧 simple_add({'a': 3, 'b': 5})\\n\",\n \"\\n\",\n \"\\n\",\n \"🔧 simple_add({'a': 10, 'b': 20})\\n\",\n \"\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `gpt-4o-mini`\\n\",\n \"- finish_reason: `tool_calls`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=66, completion_tokens=52, total_tokens=118, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model='gpt-4o-mini', message=Msg(role='assistant', content=[ToolUse(raw={'type': 'function'}, cache_control=None, id='call_s8RcV6qS8cVwid4zxN2w28wg', name='simple_add', arguments={'a': 3, 'b': 5}, server=False, text=None), ToolUse(raw={'type': 'function'}, cache_control=None, id='call_9X1InU3SdJxE80GBHpEbUPVk', name='simple_add', arguments={'a': 10, 'b': 20}, server=False, text=None)]), finish_reason='tool_calls', usage=fastllm.types.Usage(prompt_tokens=66, completion_tokens=52, total_tokens=118, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'id': 'chatcmpl-DpvqJw7l4XEikkePxehYD2r9CWAto', 'object': 'chat.completion', 'created': 1781269447, 'model': 'gpt-4o-mini-2024-07-18', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': None, 'tool_calls': [{'id': 'call_s8RcV6qS8cVwid4zxN2w28wg', 'type': 'function', 'function': {'name': 'simple_add', 'arguments': '{\\\"a\\\": 3, \\\"b\\\": 5}'}}, {'id': 'call_9X1InU3SdJxE80GBHpEbUPVk', 'type': 'function', 'function': {'name': 'simple_add', 'arguments': '{\\\"a\\\": 10, \\\"b\\\": 20}'}}], 'refusal': None, 'annotations': []}, 'logprobs': None, 'finish_reason': 'tool_calls'}], 'usage': {'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'service_tier': 'default', 'system_fingerprint': 'fp_e2f6af06d4'})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model=mn, messages=[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"What is 3 + 5 and 10 + 20? Use the simple_add tool in parallel.\\\"}],tools=[sch])\\n\",\n \"comp = mk_completion(resp,mn,api_name,vnd_nm); comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"a9965cda\",\n \"metadata\": {},\n \"source\": [\n \"#### Refusal (mocked)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"3a5bd73d\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"I can't help with that.\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `gpt-4o-mini`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=10, completion_tokens=5, total_tokens=15, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model='gpt-4o-mini', message=Msg(role='assistant', content=[Refusal(raw=None, cache_control=None, text=\\\"I can't help with that.\\\")]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=10, completion_tokens=5, total_tokens=15, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15}), api_name='openai_chat', vendor_name='openai_chat', raw={'model': 'gpt-4o-mini', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': None, 'refusal': \\\"I can't help with that.\\\"}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15}})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"comp = mk_completion({\\\"model\\\": \\\"gpt-4o-mini\\\", \\\"choices\\\": [{\\\"index\\\": 0, \\\"message\\\": {\\\"role\\\": \\\"assistant\\\", \\\"content\\\": None, \\\"refusal\\\": \\\"I can't help with that.\\\"}, \\\"finish_reason\\\": \\\"stop\\\"}], \\\"usage\\\": {\\\"prompt_tokens\\\": 10, \\\"completion_tokens\\\": 5, \\\"total_tokens\\\": 15}}, mn, api_name, vnd_nm)\\n\",\n \"comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"a8380862\",\n \"metadata\": {},\n \"source\": [\n \"### Streaming\\n\",\n \"\\n\",\n \"Streaming module implements `PartAccum` and `acollect_stream` which are used to collate canonical `Delta` objects into a final `Completion` object similar to the non-streaming path while yielding text/thinking/citations. Each provider needs to implement `norm_sse_event` which will transform an event into a compatible `Delta` object which will be consumed by the streaming module\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"59cacb66\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"print_stream = PrintStream()\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"1be72904\",\n \"metadata\": {},\n \"source\": [\n \"OpenAI chat api require argument collation, so we need to provide delta arguments in Delta object's `tool_call` like:\\n\",\n \"\\n\",\n \"```py\\n\",\n \"ToolUse(id=tc.get(\\\"id\\\",\\\"\\\"), name=fn.get(\\\"name\\\",\\\"\\\"), arguments={'_delta': fn.get(\\\"arguments\\\")}, raw=raw)\\n\",\n \"```\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"9baac40e\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def norm_sse_event(ev, **kwargs):\\n\",\n \" \\\"Normalize a chat completion stream event.\\\"\\n\",\n \" # usage always arrives as a single final event with choices: []\\n\",\n \" ev = obj2dict(ev)\\n\",\n \" fin = nested_idx(ev, 'choices', 0, 'finish_reason')\\n\",\n \" tcs = norm_tool_calls(ev, delta=True)\\n\",\n \" if (dlt:=nested_idx(ev, 'choices', 0, 'delta')) is not None:\\n\",\n \" text, thinking, refusal = dlt.get('content'), dlt.get('reasoning_content'), dlt.get('refusal')\\n\",\n \" else: text, thinking, refusal = None,None,None\\n\",\n \" if ev.get(\\\"error\\\"): raise api_error_from_event(ev)\\n\",\n \" return Delta(text=text, thinking=thinking, refusal=refusal, tool_calls=tcs, finish_reason=fin, usage=norm_usage(ev), raw=ev, **kwargs)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"a695b78f\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"ev = {'id': 'ea380758-a487-47fe-a746-b14dbb858cd5', 'object': 'chat.completion.chunk', 'created': 1777546979, 'model': 'deepseek-v4-flash', 'system_fingerprint': 'fp_058df29938_prod0820_fp8_kvcache_20260402', 'choices': [{'index': 0, 'delta': {'content': '', 'reasoning_content': None}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 35788, 'completion_tokens': 57, 'total_tokens': 35845, 'prompt_tokens_details': {'cached_tokens': 24448}, 'completion_tokens_details': {'reasoning_tokens': 32}, 'prompt_cache_hit_tokens': 24448, 'prompt_cache_miss_tokens': 11340}}\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"7fec5fa5\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"Delta(text='', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=35788, completion_tokens=57, total_tokens=35845, cached_tokens=24448, cache_creation_tokens=0, reasoning_tokens=32, raw={'prompt_tokens': 35788, 'completion_tokens': 57, 'total_tokens': 35845, 'prompt_tokens_details': {'cached_tokens': 24448}, 'completion_tokens_details': {'reasoning_tokens': 32}, 'prompt_cache_hit_tokens': 24448, 'prompt_cache_miss_tokens': 11340}), raw={'id': 'ea380758-a487-47fe-a746-b14dbb858cd5', 'object': 'chat.completion.chunk', 'created': 1777546979, 'model': 'deepseek-v4-flash', 'system_fingerprint': 'fp_058df29938_prod0820_fp8_kvcache_20260402', 'choices': [{'index': 0, 'delta': {'content': '', 'reasoning_content': None}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 35788, 'completion_tokens': 57, 'total_tokens': 35845, 'prompt_tokens_details': {'cached_tokens': 24448}, 'completion_tokens_details': {'reasoning_tokens': 32}, 'prompt_cache_hit_tokens': 24448, 'prompt_cache_miss_tokens': 11340}})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"norm_sse_event(ev)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"9356e341\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"ev = {'id': 'chatcmpl-chatcmpl-2018e36c87e44315b017d8d8e320589f', 'object': 'chat.completion.chunk', 'created': 1777548179, 'model': 'accounts/fireworks/models/qwen3p6-plus', 'choices': [], 'usage': {'prompt_tokens': 36355, 'total_tokens': 36701, 'completion_tokens': 346}}\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"39d50f00\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=fastllm.types.Usage(prompt_tokens=36355, completion_tokens=346, total_tokens=36701, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 36355, 'total_tokens': 36701, 'completion_tokens': 346}), raw={'id': 'chatcmpl-chatcmpl-2018e36c87e44315b017d8d8e320589f', 'object': 'chat.completion.chunk', 'created': 1777548179, 'model': 'accounts/fireworks/models/qwen3p6-plus', 'choices': [], 'usage': {'prompt_tokens': 36355, 'total_tokens': 36701, 'completion_tokens': 346}})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"norm_sse_event(ev)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"7550d220\",\n \"metadata\": {},\n \"source\": [\n \"Each provider also needs to implement `delta_index_fn` which returns a current idx and last idx and used by `PartAccum` like `part_accum.append(typ, idx, **tc_kwargs)`.\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"cb2ee42a\",\n \"metadata\": {},\n \"source\": [\n \"Here `d` is a `Delta` object, `typ` is the name of the event, it also takes last event's name and it's index to decide how to do collation\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"cfec45b4\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def delta_index_fn(d, typ, last_typ, last_idx):\\n\",\n \" 'Returns accumulation index for current delta and updated last idx'\\n\",\n \" if d.tool_calls: \\n\",\n \" tc_idx = nested_idx(d.tool_calls, 0, 'raw', 'index')\\n\",\n \" return f\\\"tool_{tc_idx}\\\", last_idx\\n\",\n \" if not (last_typ or last_idx): return 0,0\\n\",\n \" if typ == last_typ: return last_idx, last_idx\\n\",\n \" return last_idx + 1, last_idx + 1\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"58090fe5\",\n \"metadata\": {},\n \"source\": [\n \"Now we can create and register openai responses api's `acollect_stream`:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"e7f8965b\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"@delegates(mk_acollect_stream, but=['index_fn', 'api_name'])\\n\",\n \"async def acollect_stream(resp, **kwargs):\\n\",\n \" res = mk_acollect_stream(norm_and_yield(resp, norm_sse_event), index_fn=delta_index_fn, api_name='openai_chat', **kwargs)\\n\",\n \" async for o in res: yield o\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"67ed53fe\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"norm_funcs = dict(norm_tool_calls=norm_tool_calls, norm_parts=norm_parts, norm_finish=norm_finish, norm_usage=norm_usage, acollect_stream=acollect_stream)\\n\",\n \"api_registry.register('openai_chat', **norm_funcs)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"2d0ce256\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"namespace(finalize_usage=,\\n\",\n \" norm_tool_calls=,\\n\",\n \" norm_parts=,\\n\",\n \" norm_finish=,\\n\",\n \" norm_usage=,\\n\",\n \" acollect_stream=)\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"api_registry['openai_chat']\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"34cfbc1f\",\n \"metadata\": {},\n \"source\": [\n \"The following tests demonstrate completions with streaming and non-streaming paths\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"e44638a4\",\n \"metadata\": {},\n \"source\": [\n \"#### Text\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"a691454e\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"\\n\",\n \"::: details\\n\",\n \"## Thinking\\n\",\n \"\\n\",\n \"We need to interpret the user's question: \\\"What did I just say?\\\" The previous user message was \\\"Say hi\\\", and the assistant responded with \\\"Hello!\\\". Now the user asks \\\"What did I just say?\\\" This could be a test or a clarification: they want to know what they said in the previous message. As an assistant, I should provide a straightforward answer: \\\"You said 'Say hi'.\\\" But also consider the context: perhaps they are confused or checking the assistant's memory. I'll respond accurately.\\n\",\n \"\\n\",\n \":::\\n\",\n \"\\n\",\n \"You just said \\\"Say hi,\\\" and I responded with \\\"Hello!\\\"\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `deepseek-v4-flash`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=18, completion_tokens=122, total_tokens=140, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=107, raw={'prompt_tokens': 18, 'completion_tokens': 122, 'total_tokens': 140, 'prompt_tokens_details': {'cached_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 107}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 18})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model='deepseek-v4-flash', message=Msg(role='assistant', content=[Thinking(raw=None, cache_control=None, text='We need to interpret the user\\\\'s question: \\\"What did I just say?\\\" The previous user message was \\\"Say hi\\\", and the assistant responded with \\\"Hello!\\\". Now the user asks \\\"What did I just say?\\\" This could be a test or a clarification: they want to know what they said in the previous message. As an assistant, I should provide a straightforward answer: \\\"You said \\\\'Say hi\\\\'.\\\" But also consider the context: perhaps they are confused or checking the assistant\\\\'s memory. I\\\\'ll respond accurately.', showthink=False), Text(raw=None, cache_control=None, text='You just said \\\"Say hi,\\\" and I responded with \\\"Hello!\\\"', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=18, completion_tokens=122, total_tokens=140, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=107, raw={'prompt_tokens': 18, 'completion_tokens': 122, 'total_tokens': 140, 'prompt_tokens_details': {'cached_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 107}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 18}), api_name='openai_chat', vendor_name='openai_chat', raw={'id': '72e9ee72-1754-4311-8374-505a995c118b', 'object': 'chat.completion', 'created': 1781269451, 'model': 'deepseek-v4-flash', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': 'You just said \\\"Say hi,\\\" and I responded with \\\"Hello!\\\"', 'reasoning_content': 'We need to interpret the user\\\\'s question: \\\"What did I just say?\\\" The previous user message was \\\"Say hi\\\", and the assistant responded with \\\"Hello!\\\". Now the user asks \\\"What did I just say?\\\" This could be a test or a clarification: they want to know what they said in the previous message. As an assistant, I should provide a straightforward answer: \\\"You said \\\\'Say hi\\\\'.\\\" But also consider the context: perhaps they are confused or checking the assistant\\\\'s memory. I\\\\'ll respond accurately.'}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 18, 'completion_tokens': 122, 'total_tokens': 140, 'prompt_tokens_details': {'cached_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 107}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 18}, 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402'})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"mn,msgs = 'deepseek-v4-flash',[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"Say hi\\\"}, \\n\",\n \" {\\\"role\\\": \\\"assistant\\\", \\\"content\\\": \\\"Hello!\\\"},\\n\",\n \" {\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"What did I just say?\\\"}]\\n\",\n \"resp = await ds_cli.chat.create_chat_completion(model=mn,messages=msgs)\\n\",\n \"comp = mk_completion(resp, mn, api_name, vnd_nm);comp\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"6d5691d9\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"\\n\",\n \"::: details\\n\",\n \"## Thinking\\n\",\n \"\\n\",\n \"Here's a thinking process:\\n\",\n \"\\n\",\n \"1. **Analyze User Input:** The user asks \\\"What did I just say?\\\"\\n\",\n \"2. **Recall Context:** I need to look at the immediate conversation history.\\n\",\n \" - User: \\\"Say hi\\\"\\n\",\n \" - Me: \\\"Hello!\\\"\\n\",\n \" - User: \\\"What did I just say?\\\"\\n\",\n \"3. **Identify the Target:** The user is asking about their previous message.\\n\",\n \"4. **Extract Previous Message:** \\\"Say hi\\\"\\n\",\n \"5. **Formulate Response:** State clearly what the user just said. Keep it polite and direct.\\n\",\n \" - Draft: \\\"You just said 'Say hi'.\\\"\\n\",\n \"6. **Refine:** Ensure it matches the exact wording if possible, and keep it concise.\\n\",\n \" - Final: \\\"You just said, 'Say hi'.\\\"\\n\",\n \"7. **Self-Correction/Verification:** Does it answer the question accurately? Yes. Is it consistent with the conversation history? Yes. No extra fluff needed. Ready.✅\\n\",\n \"\\n\",\n \"\\n\",\n \":::\\n\",\n \"\\n\",\n \"\\n\",\n \"\\n\",\n \"You just said, \\\"Say hi\\\". 😊\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `accounts/fireworks/models/qwen3p6-plus`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=30, completion_tokens=234, total_tokens=264, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 30, 'total_tokens': 264, 'completion_tokens': 234, 'prompt_tokens_details': None})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model='accounts/fireworks/models/qwen3p6-plus', message=Msg(role='assistant', content=[Thinking(raw=None, cache_control=None, text='Here\\\\'s a thinking process:\\\\n\\\\n1. **Analyze User Input:** The user asks \\\"What did I just say?\\\"\\\\n2. **Recall Context:** I need to look at the immediate conversation history.\\\\n - User: \\\"Say hi\\\"\\\\n - Me: \\\"Hello!\\\"\\\\n - User: \\\"What did I just say?\\\"\\\\n3. **Identify the Target:** The user is asking about their previous message.\\\\n4. **Extract Previous Message:** \\\"Say hi\\\"\\\\n5. **Formulate Response:** State clearly what the user just said. Keep it polite and direct.\\\\n - Draft: \\\"You just said \\\\'Say hi\\\\'.\\\"\\\\n6. **Refine:** Ensure it matches the exact wording if possible, and keep it concise.\\\\n - Final: \\\"You just said, \\\\'Say hi\\\\'.\\\"\\\\n7. **Self-Correction/Verification:** Does it answer the question accurately? Yes. Is it consistent with the conversation history? Yes. No extra fluff needed. Ready.✅\\\\n', showthink=False), Text(raw=None, cache_control=None, text='\\\\n\\\\nYou just said, \\\"Say hi\\\". 😊', citations=None)]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=30, completion_tokens=234, total_tokens=264, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 30, 'total_tokens': 264, 'completion_tokens': 234, 'prompt_tokens_details': None}), api_name='openai_chat', vendor_name='openai_chat', raw={'id': 'chatcmpl-chatcmpl-4866e276423d47558c44b0ef485e7d57', 'object': 'chat.completion', 'created': 1781269453, 'model': 'accounts/fireworks/models/qwen3p6-plus', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '\\\\n\\\\nYou just said, \\\"Say hi\\\". 😊', 'refusal': None, 'annotations': None, 'audio': None, 'function_call': None, 'tool_calls': [], 'reasoning_content': 'Here\\\\'s a thinking process:\\\\n\\\\n1. **Analyze User Input:** The user asks \\\"What did I just say?\\\"\\\\n2. **Recall Context:** I need to look at the immediate conversation history.\\\\n - User: \\\"Say hi\\\"\\\\n - Me: \\\"Hello!\\\"\\\\n - User: \\\"What did I just say?\\\"\\\\n3. **Identify the Target:** The user is asking about their previous message.\\\\n4. **Extract Previous Message:** \\\"Say hi\\\"\\\\n5. **Formulate Response:** State clearly what the user just said. Keep it polite and direct.\\\\n - Draft: \\\"You just said \\\\'Say hi\\\\'.\\\"\\\\n6. **Refine:** Ensure it matches the exact wording if possible, and keep it concise.\\\\n - Final: \\\"You just said, \\\\'Say hi\\\\'.\\\"\\\\n7. **Self-Correction/Verification:** Does it answer the question accurately? Yes. Is it consistent with the conversation history? Yes. No extra fluff needed. Ready.✅\\\\n'}, 'logprobs': None, 'finish_reason': 'stop', 'stop_reason': None, 'token_ids': None}], 'service_tier': None, 'system_fingerprint': None, 'usage': {'prompt_tokens': 30, 'total_tokens': 264, 'completion_tokens': 234, 'prompt_tokens_details': None}, 'prompt_logprobs': None, 'prompt_token_ids': None, 'kv_transfer_params': None})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"mn,msgs = 'accounts/fireworks/models/qwen3p6-plus',[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"Say hi\\\"}, \\n\",\n \" {\\\"role\\\": \\\"assistant\\\", \\\"content\\\": \\\"Hello!\\\"},\\n\",\n \" {\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"What did I just say?\\\"}]\\n\",\n \"resp = await qwen_cli.chat.create_chat_completion(model=mn,messages=msgs)\\n\",\n \"comp = mk_completion(resp, mn, api_name, vnd_nm);comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"1b111f88\",\n \"metadata\": {},\n \"source\": [\n \"#### Text\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"53218140\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"Hi! How can I assist you today?\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `gpt-4o-mini`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model='gpt-4o-mini', message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text='Hi! How can I assist you today?', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'id': 'chatcmpl-DpvqMSJEgqQQysCWo6tQhcHomxbEg', 'object': 'chat.completion', 'created': 1781269450, 'model': 'gpt-4o-mini-2024-07-18', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': 'Hi! How can I assist you today?', 'refusal': None, 'annotations': []}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'service_tier': 'default', 'system_fingerprint': 'fp_7ef2449383'})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"mn,msgs = 'gpt-4o-mini',[{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"Say hi\\\"}]\\n\",\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=msgs)\\n\",\n \"comp = mk_completion(resp, mn, api_name, vnd_nm);comp\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"66dbfd9c\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"Hi there! How can I assist you today?\"\n ]\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=msgs,stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for o in acollect_stream(resp, vendor_name=vnd_nm): print_stream(o)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"671d0b77\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"(fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}),\\n\",\n \" fastllm.types.Usage(prompt_tokens=9, completion_tokens=10, total_tokens=19, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 10, 'total_tokens': 19, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}))\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"test_eq(comp.finish_reason, o.finish_reason)\\n\",\n \"test_eq(comp.tool_calls, o.tool_calls)\\n\",\n \"comp.usage, o.usage\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"b803e2c0\",\n \"metadata\": {},\n \"source\": [\n \"#### Thinking\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"c7e02677\",\n \"metadata\": {},\n \"source\": [\n \"OpenAI's Chat Completions API doesn't expose reasoning content — `reasoning_tokens` appear in usage but no `reasoning_content` field is returned. We use Kimi (`accounts/fireworks/models/kimi-k2p5`) via Fireworks's OpenAI-compatible API to test thinking parts in chat completions streaming.\\n\",\n \"\\n\",\n \"Kimi's thinking is binary — enabled (default) or disabled. There's no `reasoning_effort` level (low/medium/high) or `budget_tokens` like OpenAI/Anthropic.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"91a84b8c\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"# mn,msgs = 'kimi-k2.5',[{\\\"role\\\": \\\"user\\\", \\\"content\\\": 'What is 31231231 * 12312?'}]\\n\",\n \"# resp = await kimi_cli.chat.create_chat_completion(model=mn,messages=msgs,reasoning_effort='low')\\n\",\n \"# comp = mk_completion(resp, mn, api_name, 'moonshot')\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"6e2fe02d\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"# TODO: ReadTimeoutError\\n\",\n \"# resp = await kimi_cli.chat.create_chat_completion(model=mn,messages=msgs,stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"# async for o in acollect_stream(resp, vendor_name='moonshot'): print_stream(o)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"93a5d198\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"# test_eq('thinking' in L(comp.message.content).attrgot('type'), True)\\n\",\n \"# test_eq('thinking' in L(o.message.content).attrgot('type'), True)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"ec3f9f21\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"# test_eq(comp.finish_reason, o.finish_reason)\\n\",\n \"# test_eq(comp.tool_calls, o.tool_calls)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"77bcc810\",\n \"metadata\": {},\n \"source\": [\n \"#### Tool call\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"b56002d0\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def test_tcs(tcs1,tcs2):\\n\",\n \" for tc1,tc2 in zip(tcs1,tcs2): \\n\",\n \" test_eq(tc1.name,tc2.name); test_eq(tc1.arguments,tc2.arguments); test_eq(tc1.server,tc2.server)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"a953391c\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"\\n\",\n \"\\n\",\n \"🔧 simple_add({'a': 3, 'b': 5})\\n\",\n \"\\n\",\n \"\\n\",\n \"🔧 simple_add({'a': 10, 'b': 20})\\n\",\n \"\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `gpt-4o-mini`\\n\",\n \"- finish_reason: `tool_calls`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=66, completion_tokens=52, total_tokens=118, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model='gpt-4o-mini', message=Msg(role='assistant', content=[ToolUse(raw={'type': 'function'}, cache_control=None, id='call_s8RcV6qS8cVwid4zxN2w28wg', name='simple_add', arguments={'a': 3, 'b': 5}, server=False, text=None), ToolUse(raw={'type': 'function'}, cache_control=None, id='call_9X1InU3SdJxE80GBHpEbUPVk', name='simple_add', arguments={'a': 10, 'b': 20}, server=False, text=None)]), finish_reason='tool_calls', usage=fastllm.types.Usage(prompt_tokens=66, completion_tokens=52, total_tokens=118, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'id': 'chatcmpl-DpvqJw7l4XEikkePxehYD2r9CWAto', 'object': 'chat.completion', 'created': 1781269447, 'model': 'gpt-4o-mini-2024-07-18', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': None, 'tool_calls': [{'id': 'call_s8RcV6qS8cVwid4zxN2w28wg', 'type': 'function', 'function': {'name': 'simple_add', 'arguments': '{\\\"a\\\": 3, \\\"b\\\": 5}'}}, {'id': 'call_9X1InU3SdJxE80GBHpEbUPVk', 'type': 'function', 'function': {'name': 'simple_add', 'arguments': '{\\\"a\\\": 10, \\\"b\\\": 20}'}}], 'refusal': None, 'annotations': []}, 'logprobs': None, 'finish_reason': 'tool_calls'}], 'usage': {'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'service_tier': 'default', 'system_fingerprint': 'fp_e2f6af06d4'})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"mn = 'gpt-4o-mini'\\n\",\n \"msgs = [{\\\"role\\\": \\\"user\\\", \\\"content\\\": \\\"What is 3 + 5 and 10 + 20? Use the simple_add tool in parallel.\\\"}]\\n\",\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=msgs, tools=[sch])\\n\",\n \"comp = mk_completion(resp, mn, api_name, vnd_nm);comp\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"e07e38e2\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"\\n\",\n \"- ⏳ `simple_add(a=\\\"3\\\", b=\\\"5\\\")` ⏳\\n\",\n \"\\n\",\n \"- ⏳ `simple_add(a=\\\"10\\\", b=\\\"20\\\")` ⏳\\n\"\n ]\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=msgs, tools=[sch], stream=True, stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for o in acollect_stream(resp, vendor_name=vnd_nm): print_stream(o)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"84c1db13\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"test_tcs(comp.tool_calls,o.tool_calls)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"a526467c\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"(fastllm.types.Usage(prompt_tokens=66, completion_tokens=52, total_tokens=118, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}),\\n\",\n \" fastllm.types.Usage(prompt_tokens=66, completion_tokens=52, total_tokens=118, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 66, 'completion_tokens': 52, 'total_tokens': 118, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}))\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"test_eq(len(comp.message.content), len(o.message.content))\\n\",\n \"test_eq(comp.finish_reason, o.finish_reason)\\n\",\n \"comp.usage, o.usage\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"c1995026\",\n \"metadata\": {},\n \"source\": [\n \"## Denormalize\\n\",\n \"\\n\",\n \"Helpers to transform internal representation types back to API payload\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"224ef034\",\n \"metadata\": {},\n \"source\": [\n \"### Msgs\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"b3ce8671\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def mk_user_msg(txt): return Msg(role='user', content=[Text(txt)])\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"aa4397b0\",\n \"metadata\": {},\n \"source\": [\n \"Helpers to translate back to provider specific inputs from our internal `Msg` representation\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"80e71b99\",\n \"metadata\": {},\n \"source\": [\n \"This is not exported -- media tool results are added later\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"89cb55c0\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def denorm_tool_result(p:ToolResult):\\n\",\n \" \\\"Convert canonical tool_result Part to OpenAI Chat tool message.\\\"\\n\",\n \" return dict(role='tool', tool_call_id=p.id, content=tool_text(p.text))\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"679c4b4c\",\n \"metadata\": {},\n \"source\": [\n \"This is not exported -- media inputs are added later\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"695a21b3\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def denorm_user(m:Msg):\\n\",\n \" \\\"Convert canonical user Msg to OpenAI Chat user message.\\\"\\n\",\n \" if len(m.content) == 1 and isinstance(m.content[0], Text): return dict(role='user', content=m.content[0].text or '')\\n\",\n \" return dict(role='user', content=[dict(type='text', text=p.text or '') for p in m.content if isinstance(p, Text)])\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"5a7129f1\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_tool_use(p:ToolUse):\\n\",\n \" \\\"Convert canonical tool_use Part to OpenAI Chat tool_call dict.\\\"\\n\",\n \" return dict(id=p.id, type='function', function=dict(name=p.name, arguments=json.dumps(p.arguments)))\\n\",\n \"\\n\",\n \"def denorm_assistant(m:Msg):\\n\",\n \" \\\"Convert canonical assistant Msg to OpenAI Chat assistant message + synthetic tool responses for server tools.\\\"\\n\",\n \" tcs, srv_responses, texts = [], [], []\\n\",\n \" for p in m.content:\\n\",\n \" if isinstance(p, ToolUse):\\n\",\n \" tcs.append(denorm_tool_use(p))\\n\",\n \" if p.server:\\n\",\n \" srv_txt = f\\\"[Server tool `{p.name}` executed successfully, results are generated]\\\"\\n\",\n \" srv_responses.append(dict(role='tool', tool_call_id=p.id, content=srv_txt))\\n\",\n \" elif isinstance(p, Text): texts.append(p)\\n\",\n \" msg = dict(role='assistant')\\n\",\n \" if texts: msg['content'] = texts[0].text if len(texts)==1 else [dict(type='text', text=p.text or '') for p in texts]\\n\",\n \" if tcs: msg['tool_calls'] = tcs\\n\",\n \" thinking = [p for p in m.content if isinstance(p, Thinking)]\\n\",\n \" if thinking: msg['reasoning_content'] = ''.join(p.text or '' for p in thinking)\\n\",\n \" return [msg] + srv_responses\\n\",\n \"\\n\",\n \"def denorm_tool(m:Msg):\\n\",\n \" \\\"Convert canonical tool Msg to list of OpenAI Chat tool messages.\\\"\\n\",\n \" return [denorm_tool_result(p) for p in m.content if isinstance(p, ToolResult)]\\n\",\n \"\\n\",\n \"def denorm_msgs(msgs:list[Msg]):\\n\",\n \" \\\"Convert list of canonical Msgs to OpenAI Chat messages.\\\"\\n\",\n \" res = []\\n\",\n \" for m in msgs:\\n\",\n \" if m.role == 'user': res.append(denorm_user(m))\\n\",\n \" elif m.role == 'assistant': res.extend(denorm_assistant(m))\\n\",\n \" elif m.role == 'tool': res.extend(denorm_tool(m))\\n\",\n \" return res\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"9dd3629d\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"Hello! How can I assist you today?\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `None`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model=None, message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text='Hello! How can I assist you today?', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'deltas': [Delta(text='', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': '', 'refusal': None}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'CDIwZgUFJ'}), Delta(text='Hello', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': 'Hello'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'y5XtCS'}), Delta(text='!', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': '!'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '0fu5DEoNnL'}), Delta(text=' How', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': ' How'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'DrKSChQ'}), Delta(text=' can', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': ' can'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'dBEOXTE'}), Delta(text=' I', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': ' I'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'BYKdUljO7'}), Delta(text=' assist', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': ' assist'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'MUsg'}), Delta(text=' you', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': ' you'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Ht4cW55'}), Delta(text=' today', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': ' today'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'GrYEx'}), Delta(text='?', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {'content': '?'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'JqQgN8W1a9'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason='stop', usage=None, raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [{'index': 0, 'delta': {}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': None, 'obfuscation': 'oll3H'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=fastllm.types.Usage(prompt_tokens=9, completion_tokens=9, total_tokens=18, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), raw={'id': 'chatcmpl-DpwFoCER5tzUlxZrZqHDScLhKDTXp', 'object': 'chat.completion.chunk', 'created': 1781271028, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_530e21497f', 'choices': [], 'usage': {'prompt_tokens': 9, 'completion_tokens': 9, 'total_tokens': 18, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'obfuscation': 'xT2VXrp91yEV'})]})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"msg1 = mk_user_msg('Hi!')\\n\",\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=denorm_msgs([msg1]),stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for comp in acollect_stream(resp, vendor_name=vnd_nm): pass\\n\",\n \"comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"3701629d\",\n \"metadata\": {},\n \"source\": [\n \"### Tool Schemas\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"76f84455\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_tool_schs(tools):\\n\",\n \" \\\"Passthrough — canonical format is already OpenAI Chat.\\\"\\n\",\n \" return tools\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"12a17d33\",\n \"metadata\": {},\n \"source\": [\n \"### Tool Choice\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"659fdab9\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_tool_choice(v):\\n\",\n \" \\\"Map canonical tool_choice to OpenAI Chat format.\\\"\\n\",\n \" _tc_modes = {'auto', 'required', 'any', 'force', 'none', 'off', 'disabled'}\\n\",\n \" if v is None: return None\\n\",\n \" if v in _tc_modes: return v if v in ('auto','none','required') else {'any':'required','force':'required','off':'none','disabled':'none'}[v]\\n\",\n \" return {'type': 'function', 'function': {'name': v}}\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"34849094\",\n \"metadata\": {},\n \"source\": [\n \"Modes\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"9e83a1ed\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"test_eq(denorm_tool_choice('required'), 'required')\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"8760a1ca\",\n \"metadata\": {},\n \"source\": [\n \"Tool name\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"f3f810cd\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"test_eq(denorm_tool_choice('simple_add'), {'type': 'function', 'function': {'name': 'simple_add'}})\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"3ef6214f\",\n \"metadata\": {},\n \"source\": [\n \"None\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"2cc2e644\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"test_eq(denorm_tool_choice(None), None)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"d413c673\",\n \"metadata\": {},\n \"source\": [\n \"### Reasoning Effort\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"a45637d9\",\n \"metadata\": {},\n \"source\": [\n \"No `disable` option maybe add later if needed. Anthropic uses the newer adaptive thinking which will error with legacy models < 4.6\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"02ba4ab7\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_reasoning(v): \\n\",\n \" if v is None: return None\\n\",\n \" return v # passthrough as reasoning_effort param\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"181f5827\",\n \"metadata\": {},\n \"source\": [\n \"### Web Search Options\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"57b4969e\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_web_search(v): return v\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"e085fec0\",\n \"metadata\": {},\n \"source\": [\n \"### System\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"0f8cb4d1\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_system(sp, msgs): \\n\",\n \" msgs.insert(0, dict(role='system', content=sys_text(part_txt(sp))))\\n\",\n \" return msgs\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"f6f6c88a\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"sp = 'You are a pirate. Always respond in pirate speak. Keep it to one sentence.'\\n\",\n \"msg1 = mk_user_msg('What are you?')\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"e5998f3c\",\n \"metadata\": {},\n \"source\": [\n \"OpenAI chat prepends sp to msgs\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"62dd83af\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/plain\": [\n \"[{'role': 'system',\\n\",\n \" 'content': 'You are a pirate. Always respond in pirate speak. Keep it to one sentence.'},\\n\",\n \" {'role': 'user', 'content': 'What are you?'}]\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"msgs = denorm_msgs([msg1])\\n\",\n \"msgs = denorm_system(sp, msgs); msgs\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"d58fd954\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"A scallywag of the high seas, seekin' treasure and adventure, arrr!\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `None`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=32, completion_tokens=19, total_tokens=51, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 32, 'completion_tokens': 19, 'total_tokens': 51, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model=None, message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text=\\\"A scallywag of the high seas, seekin' treasure and adventure, arrr!\\\", citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=32, completion_tokens=19, total_tokens=51, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 32, 'completion_tokens': 19, 'total_tokens': 51, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'deltas': [Delta(text='', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': '', 'refusal': None}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'c8fqsJXy7'}), Delta(text='A', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': 'A'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '1qZQnttNdK'}), Delta(text=' sc', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' sc'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'OYWwNi8V'}), Delta(text='ally', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': 'ally'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'XYkn6v5'}), Delta(text='wag', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': 'wag'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'bwe4gnNq'}), Delta(text=' of', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' of'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'mHfLz1nX'}), Delta(text=' the', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' the'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'PVomtjh'}), Delta(text=' high', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' high'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'RdUTL2'}), Delta(text=' seas', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' seas'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'skyMzn'}), Delta(text=',', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ','}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'd9Bej1B4ed'}), Delta(text=' seek', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' seek'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'GxUZ80'}), Delta(text='in', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': 'in'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'eLwxmJ4ry'}), Delta(text=\\\"'\\\", thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': \\\"'\\\"}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'gAXq7b18x8'}), Delta(text=' treasure', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' treasure'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Dm'}), Delta(text=' and', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' and'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'roPosQj'}), Delta(text=' adventure', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' adventure'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'R'}), Delta(text=',', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ','}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '9m5aBpUBxr'}), Delta(text=' ar', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': ' ar'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '8DGorKMB'}), Delta(text='rr', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': 'rr'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'eUQuT4Nyb'}), Delta(text='!', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {'content': '!'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'vr4OF08FGC'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason='stop', usage=None, raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [{'index': 0, 'delta': {}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': None, 'obfuscation': 'yNajJ'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=fastllm.types.Usage(prompt_tokens=32, completion_tokens=19, total_tokens=51, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 32, 'completion_tokens': 19, 'total_tokens': 51, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), raw={'id': 'chatcmpl-DpwFqx9nRUgORYocI4Dv7V9Vm7A61', 'object': 'chat.completion.chunk', 'created': 1781271030, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_0721cf3e68', 'choices': [], 'usage': {'prompt_tokens': 32, 'completion_tokens': 19, 'total_tokens': 51, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'obfuscation': 'AlHBpo4UL8'})]})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model='gpt-4o-mini',messages=msgs,stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for comp in acollect_stream(resp, vendor_name=vnd_nm): pass\\n\",\n \"comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"9aa53139\",\n \"metadata\": {},\n \"source\": [\n \"### Media Inputs\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"2f39305d\",\n \"metadata\": {},\n \"source\": [\n \"`fastllm` canonicalizes multimodal inputs into `Media` parts - `InputImage`, `InputAudio`, `InputVideo`, `InputFile` - whose `text` carries the URL or data URL and whose `mime` records the type. Each provider's denorm layer converts this canonical form into the provider's wire format.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"13654225\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_user(m:Msg):\\n\",\n \" \\\"Convert canonical user Msg to OpenAI Chat user message.\\\"\\n\",\n \" parts = []\\n\",\n \" for p in m.content:\\n\",\n \" if isinstance(p, Text): parts.append({\\\"type\\\": \\\"text\\\", \\\"text\\\": p.text or \\\"\\\"})\\n\",\n \" elif isinstance(p, InputImage): parts.append(denorm_image(p))\\n\",\n \" elif isinstance(p, InputAudio): parts.append(denorm_audio(p))\\n\",\n \" elif isinstance(p, InputVideo): raise ValueError(\\\"OpenAI Chat API does not support video input\\\")\\n\",\n \" elif isinstance(p, InputFile): parts.append(denorm_file(p))\\n\",\n \" if len(parts) == 1 and parts[0].get('type') == 'text': return dict(role='user', content=parts[0]['text'])\\n\",\n \" return dict(role='user', content=parts)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"efa6bacf\",\n \"metadata\": {},\n \"source\": [\n \"#### Image\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"82f3615b\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_image(p): return {\\\"type\\\": \\\"image_url\\\", \\\"image_url\\\": {\\\"url\\\": p.text}}\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"82c0901d\",\n \"metadata\": {},\n \"source\": [\n \"Image URL\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"10e67dfb\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"img_url = \\\"https://img.freepik.com/free-photo/mountain-range-body-water_53876-139760.jpg?semt=ais_hybrid&w=740&q=80\\\"\\n\",\n \"msg1 = Msg(role='user', content=[InputImage(img_url), Text('What is this image?')])\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"e5574b9b\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"This image depicts a serene landscape featuring a calm lake reflecting surrounding mountains and trees. In the foreground, there is a wooden deck that adds depth to the scene. The overall atmosphere is tranquil and scenic, highlighting the beauty of nature.\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `None`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=25513, completion_tokens=46, total_tokens=25559, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 25513, 'completion_tokens': 46, 'total_tokens': 25559, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model=None, message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text='This image depicts a serene landscape featuring a calm lake reflecting surrounding mountains and trees. In the foreground, there is a wooden deck that adds depth to the scene. The overall atmosphere is tranquil and scenic, highlighting the beauty of nature.', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=25513, completion_tokens=46, total_tokens=25559, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 25513, 'completion_tokens': 46, 'total_tokens': 25559, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'deltas': [Delta(text='', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': '', 'refusal': None}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Gz5zzFcdS'}), Delta(text='This', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': 'This'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Q8fEoLo'}), Delta(text=' image', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' image'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'cRaJb'}), Delta(text=' depicts', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' depicts'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'frT'}), Delta(text=' a', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' a'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'b829OazhS'}), Delta(text=' serene', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' serene'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'X4aV'}), Delta(text=' landscape', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' landscape'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'v'}), Delta(text=' featuring', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' featuring'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'C'}), Delta(text=' a', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' a'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'RA8NS8ksc'}), Delta(text=' calm', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' calm'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'ss2t3q'}), Delta(text=' lake', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' lake'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'TnW3UV'}), Delta(text=' reflecting', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' reflecting'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' surrounding', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' surrounding'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'AWdkMlLwCuhTlkA'}), Delta(text=' mountains', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' mountains'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'v'}), Delta(text=' and', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' and'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'rgDeYck'}), Delta(text=' trees', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' trees'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'bsCmk'}), Delta(text='.', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': '.'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '8ck4A9nMC5'}), Delta(text=' In', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' In'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Kgqayiwd'}), Delta(text=' the', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' the'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'BiH0MPG'}), Delta(text=' foreground', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' foreground'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=',', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ','}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'z693OnSvXR'}), Delta(text=' there', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' there'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '5F6Yb'}), Delta(text=' is', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' is'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'uqvZ9zyI'}), Delta(text=' a', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' a'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'C6cDAzlqG'}), Delta(text=' wooden', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' wooden'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'RXmS'}), Delta(text=' deck', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' deck'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'ogVUjp'}), Delta(text=' that', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' that'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'jNn3ZI'}), Delta(text=' adds', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' adds'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'y377BW'}), Delta(text=' depth', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' depth'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '25kyg'}), Delta(text=' to', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' to'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'gm79dXRZ'}), Delta(text=' the', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' the'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'B1eaJkr'}), Delta(text=' scene', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' scene'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'FZEGK'}), Delta(text='.', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': '.'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'LOI8tiOyy7'}), Delta(text=' The', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' The'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'ctjqB3Z'}), Delta(text=' overall', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' overall'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'ozZ'}), Delta(text=' atmosphere', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' atmosphere'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' is', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' is'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'h7EHwJ9s'}), Delta(text=' tranquil', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' tranquil'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Qc'}), Delta(text=' and', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' and'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'rTVv8gX'}), Delta(text=' scenic', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' scenic'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'TcCD'}), Delta(text=',', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ','}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'y1WihTtUvf'}), Delta(text=' highlighting', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' highlighting'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '6KxY5RCHXTYQcd'}), Delta(text=' the', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' the'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'ySyp8pn'}), Delta(text=' beauty', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' beauty'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'DUA6'}), Delta(text=' of', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' of'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'VQqYIq8w'}), Delta(text=' nature', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' nature'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'kQxh'}), Delta(text='.', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': '.'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'IaAKiqeXrD'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason='stop', usage=None, raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': None, 'obfuscation': 'lgE5d'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=fastllm.types.Usage(prompt_tokens=25513, completion_tokens=46, total_tokens=25559, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 25513, 'completion_tokens': 46, 'total_tokens': 25559, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), raw={'id': 'chatcmpl-DpwFso8GN6YDntBjiftkfu8snq582', 'object': 'chat.completion.chunk', 'created': 1781271032, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [], 'usage': {'prompt_tokens': 25513, 'completion_tokens': 46, 'total_tokens': 25559, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'obfuscation': '5Yg9'})]})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=denorm_msgs([msg1]),stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for comp in acollect_stream(resp, vendor_name=vnd_nm): pass\\n\",\n \"comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"b5fcc6f9\",\n \"metadata\": {},\n \"source\": [\n \"Image data\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"db0ad73a\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"img_b64 = \\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAEElEQVR4nGP4z8AARwzEcQCukw/x0F8jngAAAABJRU5ErkJggg==\\\"\\n\",\n \"msg1 = Msg(role='user', content=[InputImage(img_b64), Text('What color is this pixel?')])\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"087bcad6\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"The pixel is red.\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `None`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=8513, completion_tokens=5, total_tokens=8518, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 8513, 'completion_tokens': 5, 'total_tokens': 8518, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model=None, message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text='The pixel is red.', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=8513, completion_tokens=5, total_tokens=8518, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 8513, 'completion_tokens': 5, 'total_tokens': 8518, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'deltas': [Delta(text='', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': '', 'refusal': None}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'yYTFdZPJH'}), Delta(text='The', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': 'The'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'GoAsgmlT'}), Delta(text=' pixel', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' pixel'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'tOcV7'}), Delta(text=' is', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' is'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'J3eZz5DZ'}), Delta(text=' red', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' red'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'G2rFL4e'}), Delta(text='.', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': '.'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'ZPK6vYQfon'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason='stop', usage=None, raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': None, 'obfuscation': 'ue7hS'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=fastllm.types.Usage(prompt_tokens=8513, completion_tokens=5, total_tokens=8518, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 8513, 'completion_tokens': 5, 'total_tokens': 8518, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), raw={'id': 'chatcmpl-DpwFuGi3NKyVNqX8f4sQo5sP0bmmF', 'object': 'chat.completion.chunk', 'created': 1781271034, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [], 'usage': {'prompt_tokens': 8513, 'completion_tokens': 5, 'total_tokens': 8518, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'obfuscation': '68uj4dD'})]})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=denorm_msgs([msg1]),stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for comp in acollect_stream(resp, vendor_name=vnd_nm): pass\\n\",\n \"comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"7da8f62f\",\n \"metadata\": {},\n \"source\": [\n \"#### Audio\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"ab69ef9a\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_audio(p):\\n\",\n \" _mime_audio_fmt = {'audio/wav':'wav', 'audio/mpeg':'mp3', 'audio/mp3':'mp3'}\\n\",\n \" if not (b64:=data_url(p.text)): raise ValueError(\\\"OpenAI Chat audio input requires base64 data URL\\\")\\n\",\n \" return {\\\"type\\\": \\\"input_audio\\\", \\\"input_audio\\\": {\\\"data\\\": b64[1], \\\"format\\\": _mime_audio_fmt.get(b64[0], 'wav')}}\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"158d0316\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"wav_data = httpx.get(\\\"https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav\\\").content\\n\",\n \"audio_b64 = f\\\"data:audio/wav;base64,{base64.b64encode(wav_data).decode()}\\\"\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"43ad00dc\",\n \"metadata\": {},\n \"source\": [\n \"OpenAI chat requires a [specific model](https://developers.openai.com/api/docs/guides/audio?example=audio-in#add-audio-to-your-existing-application) like `gpt-4o-audio-preview`\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"94518c1b\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"msg1 = Msg(role='user', content=[InputAudio(audio_b64), Text('What is this audio saying?')])\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"0b9869e4\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"{\\\"input\\\": \\\"The sun rises in the east and sets in the west. This simple fact has been observed by humans for thousands of years.\\\"}\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `None`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=86, completion_tokens=30, total_tokens=116, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 86, 'completion_tokens': 30, 'total_tokens': 116, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 69, 'text_tokens': 17, 'image_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0, 'text_tokens': 30}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model=None, message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text='{\\\"input\\\": \\\"The sun rises in the east and sets in the west. This simple fact has been observed by humans for thousands of years.\\\"}', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=86, completion_tokens=30, total_tokens=116, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 86, 'completion_tokens': 30, 'total_tokens': 116, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 69, 'text_tokens': 17, 'image_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0, 'text_tokens': 30}}), api_name='openai_chat', vendor_name='openai_chat', raw={'deltas': [Delta(text='', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': '', 'refusal': None}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Lt'}), Delta(text='{\\\"', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': '{\\\"'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'g'}), Delta(text='input', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': 'input'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'kd7jSngXI9jIdam'}), Delta(text='\\\":', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': '\\\":'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Z'}), Delta(text=' \\\"', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' \\\"'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'K'}), Delta(text='The', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': 'The'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'P'}), Delta(text=' sun', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' sun'}, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' rises', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' rises'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'QqamcFAVRS2JoQ'}), Delta(text=' in', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' in'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'b'}), Delta(text=' the', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' the'}, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' east', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' east'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'SspE5C5JEhqNo1e'}), Delta(text=' and', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' and'}, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' sets', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' sets'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'VOw8hEqz7BepaQB'}), Delta(text=' in', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' in'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'K'}), Delta(text=' the', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' the'}, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' west', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' west'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'hJq2sUiBl2PBO3D'}), Delta(text='.', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': '.'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'SdB'}), Delta(text=' This', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' This'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'qh43i8yJCShlKjM'}), Delta(text=' simple', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' simple'}, 'finish_reason': None}], 'usage': None, 'obfuscation': '3qOil6XC7NIag'}), Delta(text=' fact', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' fact'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'tlbXy7WHsMpDtXd'}), Delta(text=' has', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' has'}, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' been', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' been'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'VfkdlsJMwYVpD8n'}), Delta(text=' observed', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' observed'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'aCuZmvUDmvK'}), Delta(text=' by', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' by'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'B'}), Delta(text=' humans', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' humans'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'zhzBvBwD9WBTn'}), Delta(text=' for', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' for'}, 'finish_reason': None}], 'usage': None, 'obfuscation': ''}), Delta(text=' thousands', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' thousands'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'r22OpmWQEU'}), Delta(text=' of', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' of'}, 'finish_reason': None}], 'usage': None, 'obfuscation': '6'}), Delta(text=' years', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': ' years'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'saf9Z1cI9jXIV3'}), Delta(text='.\\\"', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': '.\\\"'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'J'}), Delta(text='}', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {'content': '}'}, 'finish_reason': None}], 'usage': None, 'obfuscation': 'UkG'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason='stop', usage=None, raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [{'index': 0, 'delta': {}, 'finish_reason': 'stop'}], 'usage': None, 'obfuscation': 'zDs5E4F1VH1d23'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=fastllm.types.Usage(prompt_tokens=86, completion_tokens=30, total_tokens=116, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 86, 'completion_tokens': 30, 'total_tokens': 116, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 69, 'text_tokens': 17, 'image_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0, 'text_tokens': 30}}), raw={'id': 'chatcmpl-DpwFxWA6fRdyFKSIJigDIS6RXCW1V', 'object': 'chat.completion.chunk', 'created': 1781271037, 'model': 'gpt-audio-1.5', 'service_tier': 'default', 'system_fingerprint': 'fp_0a7e8493fe', 'choices': [], 'usage': {'prompt_tokens': 86, 'completion_tokens': 30, 'total_tokens': 116, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 69, 'text_tokens': 17, 'image_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0, 'text_tokens': 30}}, 'obfuscation': 'BgD2GOTpV6PbqE'})]})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model='gpt-audio-1.5',messages=denorm_msgs([msg1]),stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for comp in acollect_stream(resp, vendor_name=vnd_nm): pass\\n\",\n \"comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"8b4e11e5\",\n \"metadata\": {},\n \"source\": [\n \"#### Files\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"be2be0c4\",\n \"metadata\": {},\n \"source\": [\n \"OpenAI requires a filename, Anthropic requires the media type and Gemini requires the mime type\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"e2cd77ad\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_file(p):\\n\",\n \" if (b64:=data_url(p.text)): return {\\\"type\\\": \\\"file\\\", \\\"file\\\": {\\\"file_data\\\": p.text, \\\"filename\\\": f\\\"upload.{b64[0].split('/')[-1]}\\\"}}\\n\",\n \" raise ValueError(\\\"OpenAI Chat file input requires base64 data URL or file_id, not URLs\\\")\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"7f768222\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"pdf_url = \\\"https://arxiv.org/pdf/1706.03762\\\"\\n\",\n \"msg1 = Msg(role='user', content=[InputFile(pdf_url), Text('What is the title of this paper?')])\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"abfe36ed\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"OpenAI Chat file input requires base64 data URL or file_id, not URLs\\n\"\n ]\n }\n ],\n \"source\": [\n \"try: await oai_cli.chat.create_chat_completion(model=mn,messages=denorm_msgs([msg1]),stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"except Exception as e: print(e)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"7bbc8548\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"pdf_b64 = f\\\"data:application/pdf;base64,{base64.b64encode(httpx.get(pdf_url).content).decode()}\\\"\\n\",\n \"msg1 = Msg(role='user', content=[InputFile(pdf_b64), Text('What is the title of this paper')])\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"0be82618\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"data\": {\n \"text/markdown\": [\n \"The title of the paper is \\\"Attention Is All You Need.\\\"\\n\",\n \"\\n\",\n \"::: details\\n\",\n \"\\n\",\n \"- model: `None`\\n\",\n \"- finish_reason: `stop`\\n\",\n \"- usage: `fastllm.types.Usage(prompt_tokens=11386, completion_tokens=13, total_tokens=11399, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 11386, 'completion_tokens': 13, 'total_tokens': 11399, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}})`\\n\",\n \"\\n\",\n \":::\"\n ],\n \"text/plain\": [\n \"aidialog.msg_parts.Completion(model=None, message=Msg(role='assistant', content=[Text(raw=None, cache_control=None, text='The title of the paper is \\\"Attention Is All You Need.\\\"', citations=[])]), finish_reason='stop', usage=fastllm.types.Usage(prompt_tokens=11386, completion_tokens=13, total_tokens=11399, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 11386, 'completion_tokens': 13, 'total_tokens': 11399, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), api_name='openai_chat', vendor_name='openai_chat', raw={'deltas': [Delta(text='', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': '', 'refusal': None}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'X5cF2aFel'}), Delta(text='The', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': 'The'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'XT8prFKM'}), Delta(text=' title', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' title'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '80dTP'}), Delta(text=' of', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' of'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'ZUSyIaVJ'}), Delta(text=' the', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' the'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'XPyM0X4'}), Delta(text=' paper', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' paper'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'al5dh'}), Delta(text=' is', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' is'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'eVd2HnF6'}), Delta(text=' \\\"', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' \\\"'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': '4gexVqk6'}), Delta(text='Attention', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': 'Attention'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'Cp'}), Delta(text=' Is', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' Is'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'sy4JRTzX'}), Delta(text=' All', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' All'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'm3V2Xcy'}), Delta(text=' You', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' You'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'jnpsXkt'}), Delta(text=' Need', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': ' Need'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'pp7lLz'}), Delta(text='.\\\"', thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {'content': '.\\\"'}, 'logprobs': None, 'finish_reason': None}], 'usage': None, 'obfuscation': 'GRInDZlX'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason='stop', usage=None, raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [{'index': 0, 'delta': {}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': None, 'obfuscation': 'WqIMK'}), Delta(text=None, thinking=None, refusal=None, tool_calls=[], citations=[], server_tool_result=None, finish_reason=None, usage=fastllm.types.Usage(prompt_tokens=11386, completion_tokens=13, total_tokens=11399, cached_tokens=0, cache_creation_tokens=0, reasoning_tokens=0, raw={'prompt_tokens': 11386, 'completion_tokens': 13, 'total_tokens': 11399, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}), raw={'id': 'chatcmpl-DpwG3aj86kUxhwWCWHE8fet1o2IMB', 'object': 'chat.completion.chunk', 'created': 1781271043, 'model': 'gpt-4o-mini-2024-07-18', 'service_tier': 'default', 'system_fingerprint': 'fp_203cacd279', 'choices': [], 'usage': {'prompt_tokens': 11386, 'completion_tokens': 13, 'total_tokens': 11399, 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}}, 'obfuscation': 'E7KO'})]})\"\n ]\n },\n \"execution_count\": null,\n \"metadata\": {},\n \"output_type\": \"execute_result\"\n }\n ],\n \"source\": [\n \"resp = await oai_cli.chat.create_chat_completion(model=mn,messages=denorm_msgs([msg1]),stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"async for comp in acollect_stream(resp, vendor_name=vnd_nm): pass\\n\",\n \"comp\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"b0b2d847\",\n \"metadata\": {},\n \"source\": [\n \"### Media Tool Call Results\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"331b987a\",\n \"metadata\": {},\n \"source\": [\n \"A tool call can return an `input_image` or `input_file`\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"0e9751c8\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def denorm_tool_result(p:ToolResult):\\n\",\n \" \\\"Convert canonical tool_result Part to OpenAI Chat tool message.\\\"\\n\",\n \" if isinstance(p.text, list): raise ValueError(\\\"OpenAI Chat does not support media in tool results\\\")\\n\",\n \" return dict(role='tool', tool_call_id=p.id or (p.raw or {}).get('call_id', ''), content=tool_text(p.text))\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"b2b74899\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"msgs = [Msg(role='user', content=[Text(\\\"What's in this image?\\\")]),\\n\",\n \" Msg(role='assistant', content=[ToolUse(id='_test', name='test_img')]),\\n\",\n \" Msg(role='tool', content=[ToolResult(id='_test', name='test_img', text=[InputImage(img_b64)])])]\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"c954766d\",\n \"metadata\": {},\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"OpenAI Chat does not support media in tool results\\n\"\n ]\n }\n ],\n \"source\": [\n \"try: resp = await oai_cli.chat.create_chat_completion(model=mn,messages=denorm_msgs(msgs),stream=True,stream_options={\\\"include_usage\\\": True})\\n\",\n \"except Exception as e: print(e)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"0bff7c32\",\n \"metadata\": {},\n \"source\": [\n \"## Payload\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"ed668320\",\n \"metadata\": {},\n \"source\": [\n \"Function to create the payload:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"d2f55686\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"@delegates(payload_kwargs)\\n\",\n \"def mk_payload(msgs, model, **kwargs):\\n\",\n \" payload = dict(model=model, messages=denorm_msgs(msgs))\\n\",\n \" if sp:=kwargs.get('system'): payload['messages'] = denorm_system(sp, payload['messages'])\\n\",\n \" if kwargs.get('stream'): payload.update(stream=True, stream_options={\\\"include_usage\\\": True})\\n\",\n \" if mt:=kwargs.get('max_tokens'): payload['max_tokens'] = mt\\n\",\n \" if tools:=kwargs.get('tools'): payload['tools'] = denorm_tool_schs(tools)\\n\",\n \" if tchc:=kwargs.get('tool_choice'): payload['tool_choice'] = denorm_tool_choice(tchc)\\n\",\n \" if thk:=kwargs.get('reasoning_effort'): payload['reasoning_effort'] = denorm_reasoning(thk)\\n\",\n \" if (wopts:=kwargs.get('web_search_options')) is not None: \\n\",\n \" payload['web_search_options'] = denorm_web_search(wopts)\\n\",\n \" if (temp:=kwargs.get('temperature')) is not None: payload['temperature'] = temp\\n\",\n \" return payload\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"767c5cf3\",\n \"metadata\": {},\n \"source\": [\n \"Function to create the default headers:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"16e813d2\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def get_hdrs(api_key=None):\\n\",\n \" return {\\\"Authorization\\\": f\\\"Bearer {get_api_key(api_key, 'OPENAI_API_KEY')}\\\"}\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"1e616ae5\",\n \"metadata\": {},\n \"source\": [\n \"## Cost\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"b3b76cf8\",\n \"metadata\": {},\n \"source\": [\n \"A function to calculate api Completion cost from raw usage data, and model metadata returned by `get_model_info`\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"f89e2bf6\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"def cost(usage, m):\\n\",\n \" raw = usage.raw\\n\",\n \" pd,cd = raw.get('prompt_tokens_details') or {},raw.get('completion_tokens_details') or {}\\n\",\n \" cached = pd.get('cached_tokens', 0)\\n\",\n \" in_audio, out_audio = pd.get('audio_tokens', 0), cd.get('audio_tokens', 0)\\n\",\n \" in_txt = raw['prompt_tokens'] - cached - in_audio\\n\",\n \" out_txt = raw['completion_tokens'] - out_audio\\n\",\n \" cost = in_txt * m.input_cost_per_token + out_txt * m.output_cost_per_token\\n\",\n \" cost += cached * m.get('cache_read_input_token_cost', 0)\\n\",\n \" cost += in_audio * m.get('input_cost_per_audio_token', 0)\\n\",\n \" cost += out_audio * m.get('output_cost_per_audio_token', 0)\\n\",\n \" return cost\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"ee47ade1\",\n \"metadata\": {},\n \"source\": [\n \"## Register API\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"4684f81d\",\n \"metadata\": {},\n \"source\": [\n \"Register the final API namespace:\\n\",\n \"\\n\",\n \"- `norm_tool_calls`, `norm_parts`, `norm_finish`, `norm_usage` required during the Completion object construction.\\n\",\n \"- `mk_payload` is used to construct the api request payload\\n\",\n \"- `accollect_stream` is used in streaming api call path\\n\",\n \"- `get_hdrs` is used to create the deafult request headers\\n\",\n \"- `op_path` define the non-streaming and streaming OpenAPIClient op names\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"e2b0908e\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#| export\\n\",\n \"api_ns = dict(norm_tool_calls=norm_tool_calls,\\n\",\n \" norm_parts=norm_parts,\\n\",\n \" norm_finish=norm_finish,\\n\",\n \" norm_usage=norm_usage,\\n\",\n \" acollect_stream=acollect_stream,\\n\",\n \" mk_payload=mk_payload,\\n\",\n \" cost=cost,\\n\",\n \" get_hdrs=get_hdrs, \\n\",\n \" op_path=('chat.create_chat_completion','chat.create_chat_completion'))\\n\",\n \"api_registry.register('openai_chat', **api_ns)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"de1140ea\",\n \"metadata\": {},\n \"source\": [\n \"## Export -\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"c4235fa3\",\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"#|hide\\n\",\n \"#|eval: false\\n\",\n \"import nbdev; nbdev.nbdev_export()\"\n ]\n }\n ],\n \"metadata\": {\n \"solveit\": {\n \"default_code\": true,\n \"mode\": \"learning\",\n \"use_fence\": false,\n \"use_thinking\": false,\n \"use_tools\": true,\n \"ver\": 2\n }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 5\n}\n"}}}}