{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "\n", "import dotenv\n", "from httpx import Timeout\n", "from langchain_core.prompts import (\n", " ChatPromptTemplate,\n", " SystemMessagePromptTemplate,\n", " HumanMessagePromptTemplate,\n", ")\n", "from langchain_openai import ChatOpenAI\n", "from pydantic import BaseModel\n", "from langchain_community.callbacks import get_openai_callback\n", "\n", "dotenv.load_dotenv()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import data.samples_to_split as samples\n", "from src.utils import GPTModels\n", "from src.text_split_chain import create_split_text_chain_v2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "chain = create_split_text_chain_v2(llm_model=GPTModels.GPT_4o)\n", "# chain = create_split_text_chain_v2(llm_model=GPTModels.GPT_4_TURBO_2024_04_09)\n", "with get_openai_callback() as cb:\n", " res = chain.invoke({'text': samples.GATSBY_2})" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "SplitTextOutputV2(text_raw='Inside, the crimson room bloomed with light. Tom and Miss Baker sat at either end of the long couch and she read aloud to him from the Saturday Evening Post—the words, murmurous and uninflected, running together in a soothing tune. The lamplight, bright on his boots and dull on the autumn-leaf yellow of her hair, glinted along the paper as she turned a page with a flutter of slender muscles in her arms.\\n\\nWhen we came in she held us silent for a moment with a lifted hand.\\n\\n“To be continued,” she said, tossing the magazine on the table, “in our very next issue.”\\n\\nHer body asserted itself with a restless movement of her knee, and she stood up.\\n\\n“Ten o’clock,” she remarked, apparently finding the time on the ceiling. “Time for this good girl to go to bed.”\\n\\n“Jordan’s going to play in the tournament tomorrow,” explained Daisy, “over at Westchester.”\\n\\n“Oh—you’re Jordan Baker.”\\n\\nI knew now why her face was familiar—its pleasing contemptuous expression had looked out at me from many rotogravure pictures of the sporting life at Asheville and Hot Springs and Palm Beach. I had heard some story of her too, a critical, unpleasant story, but what it was I had forgotten long ago.\\n\\n“Good night,” she said softly. “Wake me at eight, won’t you.”\\n\\n“If you’ll get up.”\\n\\n“I will. Good night, Mr. Carraway. See you anon.”\\n\\n“Of course you will,” confirmed Daisy. “In fact I think I’ll arrange a marriage. Come over often, Nick, and I’ll sort of—oh—fling you together. You know—lock you up accidentally in linen closets and push you out to sea in a boat, and all that sort of thing—”\\n\\n“Good night,” called Miss Baker from the stairs. “I haven’t heard a word.”\\n\\n“She’s a nice girl,” said Tom after a moment. “They oughtn’t to let her run around the country this way.”\\n\\n“Who oughtn’t to?” inquired Daisy coldly.\\n\\n“Her family.”\\n\\n“Her family is one aunt about a thousand years old. Besides, Nick’s going to look after her, aren’t you, Nick? She’s going to spend lots of weekends out here this summer. I think the home influence will be very good for her.”\\n\\nDaisy and Tom looked at each other for a moment in silence.\\n\\n“Is she from New York?” I asked quickly.\\n\\n“From Louisville. Our white girlhood was passed together there. Our beautiful white—”\\n\\n“Did you give Nick a little heart to heart talk on the veranda?” demanded Tom suddenly.\\n\\n“Did I?” She looked at me. “I can’t seem to remember, but I think we talked about the Nordic race. Yes, I’m sure we did. It sort of crept up on us and first thing you know—”\\n\\n“Don’t believe everything you hear, Nick,” he advised me.')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "characters: ['c1', 'Daisy', 'Jordan', 'Tom', 'narrator']\n", "--------------------\n", "[narrator] Inside, the crimson room bloomed with light. Tom and Miss Baker sat at either end of the long couch and she read aloud to him from the Saturday Evening Post—the words, murmurous and uninflected, running together in a soothing tune. The lamplight, bright on his boots and dull on the autumn-leaf yellow of her hair, glinted along the paper as she turned a page with a flutter of slender muscles in her arms.\n", "[narrator] When we came in she held us silent for a moment with a lifted hand.\n", "[Jordan] “To be continued,”\n", "[narrator] she said, tossing the magazine on the table,\n", "[Jordan] “in our very next issue.”\n", "[narrator] Her body asserted itself with a restless movement of her knee, and she stood up.\n", "[Jordan] “Ten o’clock,”\n", "[narrator] she remarked, apparently finding the time on the ceiling.\n", "[Jordan] “Time for this good girl to go to bed.”\n", "[Daisy] “Jordan’s going to play in the tournament tomorrow,”\n", "[narrator] explained Daisy,\n", "[Daisy] “over at Westchester.”\n", "[narrator] “Oh—you’re Jordan Baker.”\n", "[narrator] I knew now why her face was familiar—its pleasing contemptuous expression had looked out at me from many rotogravure pictures of the sporting life at Asheville and Hot Springs and Palm Beach. I had heard some story of her too, a critical, unpleasant story, but what it was I had forgotten long ago.\n", "[Jordan] “Good night,”\n", "[narrator] she said softly.\n", "[Jordan] “Wake me at eight, won’t you.”\n", "[c1] “If you’ll get up.”\n", "[Jordan] “I will. Good night, Mr. Carraway. See you anon.”\n", "[Daisy] “Of course you will,”\n", "[narrator] confirmed Daisy.\n", "[Daisy] “In fact I think I’ll arrange a marriage. Come over often, Nick, and I’ll sort of—oh—fling you together. You know—lock you up accidentally in linen closets and push you out to sea in a boat, and all that sort of thing—”\n", "[Jordan] “Good night,”\n", "[narrator] called Miss Baker from the stairs.\n", "[Jordan] “I haven’t heard a word.”\n", "[Tom] “She’s a nice girl,”\n", "[narrator] said Tom after a moment.\n", "[Tom] “They oughtn’t to let her run around the country this way.”\n", "[Daisy] “Who oughtn’t to?”\n", "[narrator] inquired Daisy coldly.\n", "[Tom] “Her family.”\n", "[Daisy] “Her family is one aunt about a thousand years old. Besides, Nick’s going to look after her, aren’t you, Nick? She’s going to spend lots of weekends out here this summer. I think the home influence will be very good for her.”\n", "[narrator] Daisy and Tom looked at each other for a moment in silence.\n", "[narrator] “Is she from New York?”\n", "[narrator] I asked quickly.\n", "[Daisy] “From Louisville. Our white girlhood was passed together there. Our beautiful white—”\n", "[Tom] “Did you give Nick a little heart to heart talk on the veranda?”\n", "[narrator] demanded Tom suddenly.\n", "[Daisy] “Did I?”\n", "[narrator] She looked at me.\n", "[Daisy] “I can’t seem to remember, but I think we talked about the Nordic race. Yes, I’m sure we did. It sort of crept up on us and first thing you know—”\n", "[Tom] “Don’t believe everything you hear, Nick,”\n", "[narrator] he advised me.\n" ] } ], "source": [ "annotated_text = res.to_character_annotated_text()\n", "print(annotated_text.to_pretty_text())" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LLM usage:\n", "\n", "Tokens Used: 1817\n", "\tPrompt Tokens: 877\n", "\tCompletion Tokens: 940\n", "Successful Requests: 1\n", "Total Cost (USD): $0.0115925\n" ] } ], "source": [ "print(f'LLM usage:\\n\\n{cb}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "ai-audio-books", "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }