File size: 3,341 Bytes
b0da33a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53f2ac0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0da33a
 
 
 
 
 
 
 
 
 
 
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
import openai
import json
import os
from dotenv import load_dotenv
#from pyairtable import Table
import requests
import streamlit as st

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
rapid_api_key = os.getenv("X-RapidAPI-Key")
#airtable_api_key = os.getenv("AIRTABLE_API_KEY")
#table = Table(airtable_api_key, "appHojHIE4y8gVBgc", "tbldUUKZFngr78ogg")

function_descriptions = [
    {
        "name": "get_stock_movers",
        "description": "Get the stocks that has biggest price/volume moves, e.g. actives, gainers, losers, etc.",
        "parameters": {
            "type": "object",
            "properties": {
            },
        }
    },
    {
        "name": "get_stock_news",
        "description": "Get the latest news for a stock",
        "parameters": {
            "type": "object",
            "properties": {
                "performanceId": {
                    "type": "string",
                    "description": "id of the stock, which is referred as performanceID in the API"
                },
            },
            "required": ["performanceId"]
        }
    },
    {
        "name": "add_stock_news_airtable",
        "description": "Add the stock, news summary & price move to Airtable",
        "parameters": {
            "type": "object",
            "properties": {
                "stock": {
                    "type": "string",
                    "description": "stock ticker"
                },
                "move": {
                    "type": "string",
                    "description": "price move in %"
                },
                "news_summary": {
                    "type": "string",
                    "description": "news summary of the stock"
                },
            }
        }
    },
]

def get_stock_news(performanceId):
    url = "https://morning-star.p.rapidapi.com/news/list"

    querystring = {"performanceId":performanceId}

    headers = {
        "X-RapidAPI-Key": rapid_api_key,
        "X-RapidAPI-Host": "morning-star.p.rapidapi.com"
    }

    response = requests.get(url, headers=headers, params=querystring)

    short_news_list = response.json()[:5]

    print("response:", response, " json response:", short_news_list)

    return short_news_list

def get_stock_movers():
    url = "https://morning-star.p.rapidapi.com/market/v2/get-movers"

    headers = {
        "X-RapidAPI-Key": rapid_api_key,
        "X-RapidAPI-Host": "morning-star.p.rapidapi.com"
    }

    response = requests.get(url, headers=headers)
    
    return response.json()

def function_call(ai_response):
    function_call = ai_response["choices"][0]["message"]["function_call"]
    function_name = function_call["name"]
    arguments = function_call["arguments"]
    if function_name == "get_stock_movers":
        return get_stock_movers()
    elif function_name == "get_stock_news":
        performanceId = eval(arguments).get("performanceId")
        return get_stock_news(performanceId)
    else:
        return

query = "Give me a summary of what happend to the tesla stocks today?"
messages = [{"role":"user","content":query}]

response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=messages,
            functions = function_descriptions,
            function_call="auto"
        ) 

st.subheader(response)