|
118 | 118 | "- Use controlled generation\n", |
119 | 119 | "- Control thinking budget\n", |
120 | 120 | "- Set safety settings\n", |
121 | | - "- Use context caching\n" |
| 121 | + "- Use context caching\n", |
| 122 | + "- Use `thought_tag_marker`\n" |
122 | 123 | ] |
123 | 124 | }, |
124 | 125 | { |
|
915 | 916 | "\n", |
916 | 917 | "Markdown(response.choices[0].message.content)" |
917 | 918 | ] |
| 919 | + }, |
| 920 | + { |
| 921 | + "cell_type": "markdown", |
| 922 | + "metadata": { |
| 923 | + "id": "6de86575b07f" |
| 924 | + }, |
| 925 | + "source": [ |
| 926 | + "### **Example**: Use `thought_tag_marker`\t\n", |
| 927 | + "\n", |
| 928 | + "This parameter is used to separate a model's thoughts from its responses for models with Thinking available. If not specified, no tags will be returned around the model's thoughts. If present, subsequent queries will strip the thought tags and mark the thoughts appropriately for context. This helps preserve the appropriate context for subsequent queries." |
| 929 | + ] |
| 930 | + }, |
| 931 | + { |
| 932 | + "cell_type": "code", |
| 933 | + "execution_count": null, |
| 934 | + "metadata": { |
| 935 | + "id": "05cb3e1c69f2" |
| 936 | + }, |
| 937 | + "outputs": [], |
| 938 | + "source": [ |
| 939 | + "THOUGHT_TAG_MARKER = \"think\" # @param {type:\"string\"}\n", |
| 940 | + "\n", |
| 941 | + "prompt = \"\"\"\n", |
| 942 | + " How many i's are in the word supercalifragilisticexpialidocious?\n", |
| 943 | + "\"\"\"\n", |
| 944 | + "\n", |
| 945 | + "response = client.chat.completions.create(\n", |
| 946 | + " model=\"google/gemini-2.5-flash-preview-05-20\",\n", |
| 947 | + " messages=[{\"role\": \"user\", \"content\": prompt}],\n", |
| 948 | + " extra_body={\n", |
| 949 | + " \"extra_body\": {\n", |
| 950 | + " \"google\": {\n", |
| 951 | + " \"thinking_config\": {\n", |
| 952 | + " \"thinking_budget\": 1024,\n", |
| 953 | + " \"include_thoughts\": True,\n", |
| 954 | + " },\n", |
| 955 | + " \"thought_tag_marker\": THOUGHT_TAG_MARKER,\n", |
| 956 | + " },\n", |
| 957 | + " },\n", |
| 958 | + " },\n", |
| 959 | + ")\n", |
| 960 | + "\n", |
| 961 | + "print(response.choices[0].message.content)" |
| 962 | + ] |
| 963 | + }, |
| 964 | + { |
| 965 | + "cell_type": "markdown", |
| 966 | + "metadata": { |
| 967 | + "id": "5f4e9c061b99" |
| 968 | + }, |
| 969 | + "source": [ |
| 970 | + "Optionally, you can use this helper function is to parse response content to separate text within specified tags and the remaining text." |
| 971 | + ] |
| 972 | + }, |
| 973 | + { |
| 974 | + "cell_type": "code", |
| 975 | + "execution_count": null, |
| 976 | + "metadata": { |
| 977 | + "id": "8bbcbac7da0f" |
| 978 | + }, |
| 979 | + "outputs": [], |
| 980 | + "source": [ |
| 981 | + "def parse_response_with_tags(\n", |
| 982 | + " response_content: str, tag_name: str\n", |
| 983 | + ") -> tuple[str | None, str]:\n", |
| 984 | + " start_tag = f\"<{tag_name}>\"\n", |
| 985 | + " end_tag = f\"</{tag_name}>\"\n", |
| 986 | + "\n", |
| 987 | + " start_index = response_content.find(start_tag)\n", |
| 988 | + " end_index = -1\n", |
| 989 | + " if start_index != -1:\n", |
| 990 | + " end_index = response_content.find(end_tag, start_index + len(start_tag))\n", |
| 991 | + "\n", |
| 992 | + " tagged_content = None\n", |
| 993 | + " main_content = response_content.strip()\n", |
| 994 | + "\n", |
| 995 | + " if start_index != -1 and end_index != -1:\n", |
| 996 | + " tagged_content = response_content[\n", |
| 997 | + " start_index + len(start_tag) : end_index\n", |
| 998 | + " ].strip()\n", |
| 999 | + " part_before_tag = response_content[:start_index].strip()\n", |
| 1000 | + " part_after_tag = response_content[end_index + len(end_tag) :].strip()\n", |
| 1001 | + "\n", |
| 1002 | + " if part_before_tag and part_after_tag:\n", |
| 1003 | + " main_content = f\"{part_before_tag}\\n\\n{part_after_tag}\"\n", |
| 1004 | + " elif part_before_tag:\n", |
| 1005 | + " main_content = part_before_tag\n", |
| 1006 | + " elif part_after_tag:\n", |
| 1007 | + " main_content = part_after_tag\n", |
| 1008 | + " else:\n", |
| 1009 | + " main_content = \"\"\n", |
| 1010 | + "\n", |
| 1011 | + " elif start_index != -1 and end_index == -1:\n", |
| 1012 | + " pass\n", |
| 1013 | + "\n", |
| 1014 | + " return tagged_content, main_content" |
| 1015 | + ] |
| 1016 | + }, |
| 1017 | + { |
| 1018 | + "cell_type": "code", |
| 1019 | + "execution_count": null, |
| 1020 | + "metadata": { |
| 1021 | + "id": "e0fd012c54f2" |
| 1022 | + }, |
| 1023 | + "outputs": [], |
| 1024 | + "source": [ |
| 1025 | + "thought, answer = parse_response_with_tags(\n", |
| 1026 | + " response.choices[0].message.content, THOUGHT_TAG_MARKER\n", |
| 1027 | + ")\n", |
| 1028 | + "\n", |
| 1029 | + "print(\"--- Thought ---\")\n", |
| 1030 | + "print(thought)\n", |
| 1031 | + "print(\"\\n--- Answer ---\")\n", |
| 1032 | + "print(answer)" |
| 1033 | + ] |
918 | 1034 | } |
919 | 1035 | ], |
920 | 1036 | "metadata": { |
|
0 commit comments