baconnier commited on
Commit
97a9107
·
verified ·
1 Parent(s): b517ae4

Upload use_example.ipynb

Browse files
Files changed (1) hide show
  1. use_example.ipynb +286 -0
use_example.ipynb ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "data": {
10
+ "text/plain": [
11
+ "True"
12
+ ]
13
+ },
14
+ "execution_count": 1,
15
+ "metadata": {},
16
+ "output_type": "execute_result"
17
+ }
18
+ ],
19
+ "source": [
20
+ "import torch\n",
21
+ "torch.cuda.is_available()"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 2,
27
+ "metadata": {},
28
+ "outputs": [
29
+ {
30
+ "name": "stdout",
31
+ "output_type": "stream",
32
+ "text": [
33
+ "CDS stands for Credit Default Swap.\n",
34
+ "A credit default swap is a financial derivative where two parties exchange or 'swap' the risk of a borrower (typically corporate or government) failing to make timely interest payments or repay the loan's principal. The protection buyer pays a premium to the protection seller, and in return, they receive payments if the debtor defaults. CDS are commonly used by banks and investors to hedge against credit risk.\n",
35
+ "CDS can be traded on exchanges or over-the-counter (OTC).\n"
36
+ ]
37
+ }
38
+ ],
39
+ "source": [
40
+ "# Example: reuse your existing OpenAI setup\n",
41
+ "from openai import OpenAI\n",
42
+ "\n",
43
+ "# Point to the local server\n",
44
+ "client = OpenAI(base_url=\"http://localhost:1965/v1\", api_key=\"lm-studio\")\n",
45
+ "\n",
46
+ "completion = client.chat.completions.create(\n",
47
+ " model=\"baconnier/finance_dolphin_orpo_llama3_8B_r64_51K_GGUF-unsloth.Q4_K_M\",\n",
48
+ " messages=[\n",
49
+ " {\"role\": \"system\", \"content\": \"Act as a senior banker answering in 3 sentences in bullet points format.\"},\n",
50
+ " {\"role\": \"user\", \"content\": \"What is CDS, a swap ?\"}\n",
51
+ " ],\n",
52
+ "# stream=False,\n",
53
+ " max_tokens=200,\n",
54
+ " temperature=0.7,\n",
55
+ ")\n",
56
+ "print(completion.choices[0].message.content)"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": 1,
62
+ "metadata": {},
63
+ "outputs": [
64
+ {
65
+ "name": "stdout",
66
+ "output_type": "stream",
67
+ "text": [
68
+ "CDS:\n",
69
+ "- Credit Default Swap (CDS) is a financial derivative contract between two parties (buyer and seller) for insurance against default or credit risk associated with a bond or loan\n",
70
+ "- Protection buyer pays a premium to protection seller in exchange for the right to receive payment if a credit event occurs\n",
71
+ "- Protection seller is usually a financial institution, while protection buyer can be an investor or a bank\n",
72
+ "Swap:\n",
73
+ "- Swap is an agreement between two parties to exchange cash flows, typically interest payments or principal at a future date\n",
74
+ "- Common types of swaps include interest rate swaps, currency swaps, and commodity swaps\n",
75
+ "CDS differs from swaps as it focuses on credit risk protection, while swaps involve cash flow exchange.\n"
76
+ ]
77
+ }
78
+ ],
79
+ "source": [
80
+ "# Example: reuse your existing OpenAI setup\n",
81
+ "from openai import OpenAI\n",
82
+ "\n",
83
+ "# Point to the local server\n",
84
+ "client = OpenAI(base_url=\"http://localhost:8080/v1\", api_key=\"TGI\")\n",
85
+ "\n",
86
+ "completion = client.chat.completions.create(\n",
87
+ " model=\"baconnier/finance_dolphin_orpo_llama3_8B_r64_51K_GGUF-unsloth.Q4_K_M\",\n",
88
+ " messages=[\n",
89
+ " {\"role\": \"system\", \"content\": \"Act as a senior banker answering in 3 sentences in bullet points format.\"},\n",
90
+ " {\"role\": \"user\", \"content\": \" What is CDS compare it to a swap \"},\n",
91
+ " {\"role\": \"assistant\", \"content\": \"\"}\n",
92
+ " ],\n",
93
+ " max_tokens=400,\n",
94
+ " temperature=0.7,\n",
95
+ ")\n",
96
+ "\n",
97
+ "print(completion.choices[0].message.content)"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 4,
103
+ "metadata": {},
104
+ "outputs": [
105
+ {
106
+ "data": {
107
+ "application/vnd.jupyter.widget-view+json": {
108
+ "model_id": "cb879ded56fd479bb288dbdb9c5c2448",
109
+ "version_major": 2,
110
+ "version_minor": 0
111
+ },
112
+ "text/plain": [
113
+ "Loading checkpoint shards: 0%| | 0/4 [00:00<?, ?it/s]"
114
+ ]
115
+ },
116
+ "metadata": {},
117
+ "output_type": "display_data"
118
+ },
119
+ {
120
+ "name": "stderr",
121
+ "output_type": "stream",
122
+ "text": [
123
+ "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
124
+ ]
125
+ }
126
+ ],
127
+ "source": [
128
+ "import transformers\n",
129
+ "\n",
130
+ "model_id = \"baconnier/finance_dolphin_orpo_llama3_8B_r64_51K\"\n",
131
+ "\n",
132
+ "pipeline = transformers.pipeline(\n",
133
+ " \"text-generation\",\n",
134
+ " model=model_id,\n",
135
+ " model_kwargs={\"torch_dtype\": torch.bfloat16},\n",
136
+ " device=\"cuda\",\n",
137
+ ")\n"
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "code",
142
+ "execution_count": 5,
143
+ "metadata": {},
144
+ "outputs": [
145
+ {
146
+ "name": "stdout",
147
+ "output_type": "stream",
148
+ "text": [
149
+ "<|im_start|>user\n",
150
+ "Act as an exotic structurator and do not hesitate to merge standard and exotic products.\n",
151
+ "Currently, inflation is 5% and 1-year swaps are valued at 6%.\n",
152
+ "I expect inflation to reach 15% by the end of the year.\n",
153
+ "Can you create the 10 most complicated structured derivative products to handle this scenario?\n",
154
+ "Rank them by profitability, give me a score for profitability and another for risk from 0 to 10.\n",
155
+ "Add an explaination for each structured products in maximum of 3 sentences.\n",
156
+ "Think step by step and give me a concise, bulleted answer.<|im_end|>\n",
157
+ "<|im_start|>assistant\n",
158
+ "\n"
159
+ ]
160
+ }
161
+ ],
162
+ "source": [
163
+ "messages = [\n",
164
+ " {\"role\": \"user\", \"content\": '''Act as an exotic structurator and do not hesitate to merge standard and exotic products.\n",
165
+ "Currently, inflation is 5% and 1-year swaps are valued at 6%.\n",
166
+ "I expect inflation to reach 15% by the end of the year.\n",
167
+ "Can you create the 10 most complicated structured derivative products to handle this scenario?\n",
168
+ "Rank them by profitability, give me a score for profitability and another for risk from 0 to 10.\n",
169
+ "Add an explaination for each structured products in maximum of 3 sentences.\n",
170
+ "Think step by step and give me a concise, bulleted answer.'''},\n",
171
+ "]\n",
172
+ "\n",
173
+ "prompt = pipeline.tokenizer.apply_chat_template(\n",
174
+ " messages, \n",
175
+ " tokenize=False, \n",
176
+ " add_generation_prompt=True\n",
177
+ ")\n",
178
+ "\n",
179
+ "print(prompt)"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "execution_count": 6,
185
+ "metadata": {},
186
+ "outputs": [
187
+ {
188
+ "name": "stdout",
189
+ "output_type": "stream",
190
+ "text": [
191
+ "To handle the scenario of inflation reaching 15% by the end of the year, I can create the following 10 most complicated structured derivative products, ranked by profitability and risk:\n",
192
+ "\n",
193
+ "1. Inflation-linked swap spread strategy (Profitability: 9, Risk: 7)\n",
194
+ " - Buy 1-year swaps and sell 15-year swaps to profit from the expected inflation increase.\n",
195
+ " - Risk is moderate due to interest rate and inflation rate changes.\n",
196
+ "\n",
197
+ "2. Inflation-linked bond spread strategy (Profitability: 8, Risk: 6)\n",
198
+ " - Buy inflation-linked bonds and sell conventional bonds to profit from the inflation increase.\n",
199
+ " - Risk is lower than the swap spread strategy due to the fixed maturity of bonds.\n",
200
+ "\n",
201
+ "3. Inflation-linked option strategy (Profitability: 7, Risk: 5)\n",
202
+ " - Buy call options on inflation-linked bonds to profit from the expected inflation increase.\n",
203
+ " - Risk is lower than the swap spread strategy due to the limited downside.\n",
204
+ "\n",
205
+ "4. Inflation-linked credit spread strategy (Profitability: 6, Risk: 4)\n",
206
+ " - Buy high-yield inflation-linked bonds and sell investment-grade inflation-linked bonds to profit from the expected inflation increase.\n",
207
+ " - Risk is moderate due to credit risk and interest rate changes.\n",
208
+ "\n",
209
+ "5. Inflation-linked swap straddle strategy (Profitability: 5, Risk: 3)\n",
210
+ " - Buy and sell 1-year and 15-year inflation swaps simultaneously to profit from the expected inflation increase.\n",
211
+ " - Risk is moderate due to interest rate and inflation rate changes.\n",
212
+ "\n",
213
+ "6. Inflation-linked bond straddle strategy (Profitability: 4, Risk: 2)\n",
214
+ " - Buy and sell inflation-linked bonds with different maturities to profit from the expected inflation increase.\n",
215
+ " - Risk is lower than the swap straddle strategy due to the fixed maturity of bonds.\n",
216
+ "\n",
217
+ "7. Inflation-linked option straddle strategy (Profitability: 3, Risk: 1)\n",
218
+ " - Buy and sell call options on inflation-linked bonds with different maturities to profit from the expected inflation increase.\n",
219
+ " - Risk is lower than the swap straddle strategy due to the limited downside.\n",
220
+ "\n",
221
+ "8. Inflation-linked credit spread straddle strategy (Profitability: 2, Risk: 0)\n",
222
+ " - Buy and sell high-yield and investment-grade inflation-linked bonds with different maturities to profit from the expected inflation increase.\n",
223
+ " - Risk is low due to the fixed maturity of bonds and the limited downside.\n",
224
+ "\n",
225
+ "9. Inflation-linked swap spread strangle strategy (Profitability: 1, Risk: -1)\n",
226
+ " - Buy and sell 1-year and 15-year inflation swaps with different strike prices to profit from the expected inflation increase.\n",
227
+ " - Risk is high due to interest rate and inflation rate changes.\n",
228
+ "\n",
229
+ "10. Inflation-linked bond spread strangle strategy (Profitability: 0, Risk: -2)\n",
230
+ " - Buy and sell inflation-linked bonds with different strike prices to profit from the expected inflation increase.\n",
231
+ " - Risk is very high due to the limited downside and the potential for significant losses.\n",
232
+ "\n",
233
+ "The most profitable strategies are the inflation-linked swap spread strategy and the inflation-linked bond spread strategy, with a profitability score of 9 and 8, respectively. The least profitable strategy is the inflation-linked bond spread strangle strategy, with a profitability score of 0. The most risky strategies are the inflation-linked swap spread strangle strategy and the inflation-linked bond spread strangle strategy, with a risk score of -1 and -2, respectively. The least risky strategy is the inflation-linked credit spread straddle strategy, with a risk score of 0.\n",
234
+ "The 10 most complicated structured derivative products to handle the scenario of inflation reaching 15% by the end of the year are ranked by profitability and risk. The most profitable strategies are the inflation-linked swap spread strategy and the inflation-linked bond spread strategy, with a profitability score of 9 and 8, respectively. The least profitable strategy is the inflation-linked bond spread strangle strategy, with a profitability score of 0. The most risky strategies are the inflation-linked swap spread strangle strategy and the inflation-linked bond spread strangle strategy, with a risk score of -1 and -2, respectively. The least risky strategy is the inflation-linked credit spread straddle strategy, with a risk score of 0.\n"
235
+ ]
236
+ }
237
+ ],
238
+ "source": [
239
+ "\n",
240
+ "outputs = pipeline(\n",
241
+ " prompt,\n",
242
+ " max_new_tokens=2048,\n",
243
+ " do_sample=True,\n",
244
+ " temperature=0.6,\n",
245
+ " top_p=0.1,\n",
246
+ ")\n",
247
+ "print(outputs[0][\"generated_text\"][len(prompt):])"
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "code",
252
+ "execution_count": null,
253
+ "metadata": {},
254
+ "outputs": [],
255
+ "source": []
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": null,
260
+ "metadata": {},
261
+ "outputs": [],
262
+ "source": []
263
+ }
264
+ ],
265
+ "metadata": {
266
+ "kernelspec": {
267
+ "display_name": "base",
268
+ "language": "python",
269
+ "name": "python3"
270
+ },
271
+ "language_info": {
272
+ "codemirror_mode": {
273
+ "name": "ipython",
274
+ "version": 3
275
+ },
276
+ "file_extension": ".py",
277
+ "mimetype": "text/x-python",
278
+ "name": "python",
279
+ "nbconvert_exporter": "python",
280
+ "pygments_lexer": "ipython3",
281
+ "version": "3.11.3"
282
+ }
283
+ },
284
+ "nbformat": 4,
285
+ "nbformat_minor": 2
286
+ }