Skip to content

Instantly share code, notes, and snippets.

@Xe
Created December 23, 2024 05:29
Show Gist options
  • Save Xe/e00db4cd5e347ed9f19ba14f2f027684 to your computer and use it in GitHub Desktop.
Save Xe/e00db4cd5e347ed9f19ba14f2f027684 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 44,
"id": "cabcb941-f2ef-489c-b05f-746ed39b5713",
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset, disable_caching\n",
"from enum import Enum\n",
"from ollama import chat, pull\n",
"from pydantic import BaseModel, Field\n",
"from typing import List, Dict\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4114ecaa-f71c-43b7-b659-b63ef6dc805d",
"metadata": {},
"outputs": [],
"source": [
"OLLAMA_MODEL = \"hermes3\""
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "054d0174-a342-4c43-9afa-93b29626c59b",
"metadata": {},
"outputs": [],
"source": [
"class Gender(str, Enum):\n",
" male = \"male\"\n",
" female = \"female\"\n",
" agender = \"agender\"\n",
" nonbinary = \"nonbinary\"\n",
" either = \"character has no inherent gender\"\n",
"\n",
"class MediaCharacter(BaseModel):\n",
" character_name: str = Field(..., description=\"The name of the character\")\n",
" character_gender: Gender = Field(..., description=\"The typical gender of the character, if it can vary answer that it has no inherent gender\")\n",
" series_name: str = Field(..., description=\"The series the character normally comes from, with preference to the oldest series\")\n",
" genre: List[str] = Field(..., description=\"Two or three lower-case genres the series falls into, if not known then say \\\"unknown\\\"\")"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "e416b0d4-69a0-4d57-af84-fe92ccaaa23d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"character_name\":\"Twilight Sparkle\",\"character_gender\":\"female\",\"series_name\":\"My Little Pony: Friendship is Magic\",\"genre\":[\"Fantasy\",\"Adventure\"]}\n",
"{\"character_name\":\"Fluttershy\",\"character_gender\":\"female\",\"series_name\":\"My Little Pony: Friendship Is Magic\",\"genre\":[\"Animation\",\"Comedy\"]}\n",
"{\"character_name\":\"Megumin\",\"character_gender\":\"female\",\"series_name\":\"Konosuba\",\"genre\":[\"comedy\",\"fantasy\"]}\n",
"{\"character_name\":\"Monika\",\"character_gender\":\"female\",\"series_name\":\"Doki Doki Literature Club\",\"genre\":[\"visual novel\",\"horror\"]}\n",
"{\"character_name\":\"Ashe\",\"character_gender\":\"female\",\"series_name\":\"League of Legends\",\"genre\":[\"fantasy\",\"game\"]}\n",
"{\"character_name\":\"Claptrap\",\"character_gender\":\"male\",\"series_name\":\"Borderlands\",\"genre\":[\"action\",\"shooter\",\"sci-fi\"]}\n",
"{\"character_name\":\"Caterpie\",\"character_gender\":\"agender\",\"series_name\":\"Pokémon\",\"genre\":[\"Adventure\",\"Children\"]}\n"
]
}
],
"source": [
"entries = [\n",
" \"Twilight Sparkle from My Little Pony: Friendship is Magic\",\n",
" \"Fluttershy\",\n",
" \"Megumin in Konosuba\",\n",
" \"Monika from DDLC\",\n",
" \"Ashe from league\",\n",
" \"Claptrap Borderlands\",\n",
" \"Caterpie from Viridian Forest\",\n",
"]\n",
"results = []\n",
"\n",
"for x in entries:\n",
" response = chat(\n",
" messages = [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": f\"\"\"You are a helpful assistant that answers in JSON. Here's the json schema you must adhere to:\n",
"<schema>\n",
"{MediaCharacter.model_json_schema()}\n",
"</schema>\n",
"\n",
"You have been given the answer to the following question: What character from which media are you based on?\"\"\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": x,\n",
" }\n",
" ],\n",
" model=OLLAMA_MODEL,\n",
" format=MediaCharacter.model_json_schema(),\n",
" )\n",
"\n",
" character = MediaCharacter.model_validate_json(response.message.content)\n",
" results.append(character.model_dump_json())\n",
"\n",
"for x in results:\n",
" print(x)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment