{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "efbec4bf", "metadata": {}, "outputs": [], "source": [ "from groq import Groq\n", "from typing import Literal\n", "import os" ] }, { "cell_type": "code", "execution_count": 2, "id": "e8943c7d", "metadata": {}, "outputs": [], "source": [ "os.environ[\"GROQ_API_KEY\"] = \"YOUR GROQ KEY HERE\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "394c1f72", "metadata": {}, "outputs": [], "source": [ "def build_prompt(question, mode: Literal[\"cot\",\"base\"]=\"base\"):\n", "\n", " if mode==\"cot\":\n", " return f\"Q: {question}\\nLet's think step by step:\\n\"\n", " else:\n", " return f\"Q: {question}\\nA:\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "5d6dc71b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Since K is the husband of R, and V is the son of K, then R is the mother of V.\n" ] } ], "source": [ "question = \"If V is the son of K, and K is the father of S and husband of R, then who is R to V\"\n", "\n", "cot_prompt = build_prompt(question, mode=\"base\")\n", "\n", "client = Groq()\n", "response = client.chat.completions.create(\n", " model=\"llama3-8b-8192\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": cot_prompt}\n", " ],\n", " temperature=0.7,\n", " max_tokens=200,\n", ")\n", "\n", "print(response.choices[0].message.content)" ] }, { "cell_type": "code", "execution_count": 5, "id": "67b9d179", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Since K is the husband of R, and V is the son of K, then R is the mother of V.'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.choices[0].message.content.strip().split(\"\\n\")[-1]" ] }, { "cell_type": "code", "execution_count": 6, "id": "e84a6a58", "metadata": {}, "outputs": [], "source": [ "from src.cot import generate_answer\n", "from src.consistency import self_consistent_answer" ] }, { "cell_type": "code", "execution_count": 7, "id": "c0b3a1b5", "metadata": {}, "outputs": [], "source": [ "ans = generate_answer(question, mode=\"cot\",zero_shot=True)" ] }, { "cell_type": "code", "execution_count": 8, "id": "e9752254", "metadata": {}, "outputs": [], "source": [ "ans2 = self_consistent_answer(question)" ] }, { "cell_type": "code", "execution_count": 10, "id": "3678816d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([\"Let's break it down:\\n\\n* V is the son of K, so K is V's parent.\\n* K is the father of S, so K is S's parent.\\n* K is the husband of R, which means R is K's spouse.\\n\\nSince K is V's parent, and K is married to R, it means R is V's grandparent. Specifically, R is V's mother's mother, or V's maternal grandmother.\",\n", " 'Since K is the husband of R, and V is the son of K, that makes R the mother of V.',\n", " 'Since K is the husband of R, and V is the son of K, then R is the mother of V.'],\n", " [\"Since K is V's parent, and K is married to R, it means R is V's grandparent. Specifically, R is V's mother's mother, or V's maternal grandmother.\",\n", " 'Since K is the husband of R, and V is the son of K, that makes R the mother of V.',\n", " 'Since K is the husband of R, and V is the son of K, then R is the mother of V.'])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ans2" ] }, { "cell_type": "code", "execution_count": null, "id": "72fd9b9d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "LLM_reasoning", "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.10.18" } }, "nbformat": 4, "nbformat_minor": 5 }