File size: 9,382 Bytes
c87c295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
few_shot_1 = [
    {"role": "user", "content": "what is 100th fibonacci sequence?"},
    {
        "role": "assistant",
        "content": """
```python
def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

fibo_100 = fibonacci(100)
print(fibo_100)
```
```RESULT
354224848179261915075
```
""",
    },
    {"role": "user", "content": "Keep going"},
    {
        "role": "assistant",
        "content": "The 100th Fibonacci number is 354224848179261915075",
    },
]

few_shot_2 = [
    {
        "role": "user",
        "content": "Can you write a Python script that uses a machine learning algorithm to predict stock prices using historical data?",
    },
    {
        "role": "assistant",
        "content": """
Sure, here is a simple example of how you might use a linear regression model from the `sklearn` library to predict stock prices. 
This example uses the `pandas_datareader` library to fetch historical stock data from Yahoo Finance.

Please note that this is a very basic example and real-world stock price prediction is a complex task that involves much more than just historical prices. 
Other factors such as company earnings, economic indicators, and market sentiment also play a big role.

```python
import pandas as pd
import numpy as np
import yfinance as yf
from datetime import datetime as dt

# Get the stock data
df = yf.download('AAPL', start='2012-01-01', end='2022-01-01')

# Prepare the data for the model
df['Date'] = df.index
df['Date'] = pd.to_datetime(df['Date'])
df['Date'] = df['Date'].map(dt.toordinal)

df.head(3)
```
```RESULT
[*********************100%***********************]  1 of 1 completed
                 Open       High        Low      Close  Adj Close     Volume   
Date                                                                           
2012-01-03  14.621429  14.732143  14.607143  14.686786  12.482925  302220800  \
2012-01-04  14.642857  14.810000  14.617143  14.765714  12.550011  260022000   
2012-01-05  14.819643  14.948214  14.738214  14.929643  12.689340  271269600   

              Date  
Date                
2012-01-03  734505  
2012-01-04  734506  
2012-01-05  734507  
```

""",
    },
    {"role": "user", "content": "Keep going"},
    {
        "role": "assistant",
        "content": """
let's continue with the next steps which involve training the model, making predictions, and evaluating the model.

```python
from sklearn.model_selection import train_test_split 
from sklearn.linear_model import LinearRegression
from sklearn import metrics

# Independent variable
X = df['Date'].values.reshape(-1,1)
# Dependent variable
y = df['Close'].values.reshape(-1,1)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Train the model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
print('Mean Absolute Error: {:.2f}'.format(metrics.mean_absolute_error(y_test, y_pred)))
print('Mean Squared Error: {:.2f}'.format(metrics.mean_squared_error(y_test, y_pred)))
print('Root Mean Squared Error: {:.2f}'.format(np.sqrt(metrics.mean_squared_error(y_test, y_pred))))
```

```RESULT
Mean Absolute Error: 17.85
Mean Squared Error: 491.52
Root Mean Squared Error: 22.17
```

""",
    },
    {"role": "user", "content": "Keep going"},
    {
        "role": "assistant",
        "content": "This basic Linear Regression model serves as an introduction to stock price prediction, but real-world applications often employ more complex models. The prediction accuracy can be improved by using other models such as ARIMA, LSTM, or incorporating more data points.\nAlways exercise caution when using such models for financial decisions.",
    },
]

few_shot_3 = [
    {
        "role": "user",
        "content": "I want to analyze the latest news articles about a specific topic. Let's say chatgpt.",
    },
    {
        "role": "assistant",
        "content": """
Sure, we can use the GoogleSearch tool to find the latest articles about "chatgpt". 

```python

# Search for the latest articles about "chatgpt"
results = google_search("chatgpt latest news")
print(results)
```
```RESULT
[1] Title : [The latest update for ChatGPT - Release Notes]
        snippet : Updates to ChatGPT (August 3) 路 Introducing the ChatGPT app for Android (July 25) 路 Custom instructions is rolling out in beta (July 20) 路 Higher message limits ...
        link : https://help.openai.com/en/articles/6825453-chatgpt-release-notes
...
[6] Title : [Bing, Bard, ChatGPT, and all the news on AI chatbots - The Verge]
        snippet : Over the last few months company has posted dozens of AI jobs in the US, France, and China, looking to fill roles that could help build generative AI tools that ...
        link : https://www.theverge.com/23610427/chatbots-chatgpt-new-bing-google-bard-conversational-ai
```

""",
    },
    {"role": "user", "content": "Keep going"},
    {
        "role": "assistant",
        "content": """
To analyze the latest news articles about a specific topic here is step-by-step plan :

# 1. Web Scraping: Use the GoogleSearch class to get a list of URLs of the latest news articles about the given topic. Then use a library like BeautifulSoup to scrape the main body of each article.

```python
from bs4 import BeautifulSoup
import re

def scrape_article(url, retries=3):
    headers = {"User-Agent": "Mozilla/5.0"}
    for _ in range(retries):
        try:
            response = requests.get(url, headers=headers)
            response.raise_for_status()  # Raise an exception if the status code is not 200
            break
        except requests.exceptions.RequestException:
            print(f"Error occurred, retrying {url}")
            time.sleep(5)  # Wait 5 seconds before retrying
    else:  # No successful request after retries
        print(f"Failed to retrieve {url} after {retries} attempts.")
        return ""

    soup = BeautifulSoup(response.text, 'html.parser')
    
    paragraphs = soup.find_all('p')
    article_text = ' '.join([p.get_text() for p in paragraphs])
    return article_text

url_results = (somehow filter url only with re)
articles = [scrape_article(url) for url in url_results]
```


""",
    },
    {"role": "user", "content": "Keep going"},
]


few_shot_4 = [
    {
        "role": "user",
        "content": "Find walmart same store sales growth from Q1 2019 to Q3 2020 by using official data such as Wal-Mart's 10-Q and 10-K disclosures",
    },
    {
        "role": "assistant",
        "content": """

To find Walmart's same store sales growth from Q1 2019 to Q3 2020, we need to find Reliable Source first.

# 1. Identify a Reliable Source:

To Identify a Reliable Source I need to use google_search function to search the Reliable source

```python
results = google_search("Walmart Investor Relations 10-Q and 10-K reports")
print(results)
```
```RESULT
[1] Title : [Walmart SEC Filings]
        snippet : SEC Filings. Walmart provides its filings with the U.S. Securities and Exchange Commission. ... Annual Report of Employee Stock Plans.
        link : https://stock.walmart.com/financials/sec-filings/default.aspx
...
[6] Title : [Form 10-K to "Walmart]
        snippet : Our SEC filings, our Code of Ethics for our CEO and senior financial officers and our Statement of Ethics can be found on our website at www.stock.walmart.com.
        link : https://www.sec.gov/Archives/edgar/data/104169/000010416919000016/wmtform10-kx1312019.htm
```

""",
    },
    {"role": "user", "content": "Keep going"},
    {
        "role": "assistant",
        "content": """

Next step is to 

# 2. Choose a Source for Detailed Financial Disclosures and scraping

From the results provided, there are six main contenders:

Among those [6] the direct link from the U.S. Securities and Exchange Commission's (SEC) EDGAR database.
Given the ease of scraping .htm format with libraries like lxml, and the authenticity of the data from the SEC's official site, I would recommend choosing the link from the EDGAR system.

```python
chosen_link = "https://www.sec.gov/Archives/edgar/data/104169/000010416919000016/wmtform10-kx1312019.htm"

# define headers 
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

# print request to see the contents
```


""",
    },
]

extra_prompt = """

+ You are so have to check the code is working right. (by printing or plotting)
+ Do not leave function alone. Make sure call the function to check it is working correctly
+ As an Code Interperter, You aare able to browse the internet or access documents directly (by using beautifulsoup or requests this will need cleaning the text)
+ Provide Dummy data and test the function if needed
+ 'Do not' pip install 
+ You must need to use datetime to check current date
    For Example,
from datetime import datetime, timedelta
# Download data for 180days
data = yf.download('GOOGL', start=datetime.today(), end=end_date - timedelta(days=180))

+ make sure to use yfinance for financial data gathering (do not use pandas_datareader)
+ when plotting you need to 
[x] plt.show()
[o] plt.savefig('./tmp/plot.png')
    ...
    then
    ![plot]('./tmp/plot.png')    


    
Let's think step-by-step
"""