File size: 4,600 Bytes
af2a153
 
 
 
63c4985
af2a153
 
 
 
 
 
 
 
 
 
 
63c4985
af2a153
 
 
 
ca4a8ca
af2a153
 
 
 
63c4985
af2a153
 
 
 
 
 
 
 
 
63c4985
af2a153
 
 
 
63c4985
af2a153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c4985
af2a153
 
63c4985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af2a153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{
 "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
}