Created
November 6, 2023 05:31
-
-
Save Kurry/580c99c5feae13aed561c1f5fc7b7f10 to your computer and use it in GitHub Desktop.
Full Code for DIY BloombergGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import bql | |
from dotenv import load_dotenv | |
from pandasai import SmartDataframe, Config | |
from pandasai.llm.openai import OpenAI | |
import openai | |
# Set your API key | |
openai.api_key = os.getenv('OPENAI_API_KEY') | |
# Ensure your .env file has the OPENAI_API_KEY variable set | |
load_dotenv() | |
class BloombergGPT: | |
def __init__(self): | |
self.url = "https://api.promptperfect.jina.ai/your-prompt-as-a-service" | |
self.headers = {"Content-Type": "application/json"} | |
self.bq = bql.Service() | |
self.llm = OpenAI() # no need to pass the API key, it will be read from the environment variable | |
def get_bql_function_syntax(self, query): | |
""" | |
Send a natural language query and receive the function syntax for the requested data. | |
:param query: A string containing the natural language request. | |
:return: A string containing the function syntax or an error message. | |
""" | |
response = requests.post(self.url, headers=self.headers, json={"parameters": {"request": query}}) | |
if response.status_code == 200: | |
data = response.json().get('data') | |
# Extract the code snippet between triple backticks | |
code_snippet = data.split('```')[1].replace('\n', '') if '```' in data else 'No code snippet provided in ' \ | |
'response. ' | |
return code_snippet | |
else: | |
return f"Error: {response.text}" | |
def generate_bql_natural_language_query(self, user_query): | |
""" | |
Uses the OpenAI API to generate a natural language BQL query from a user prompt. | |
Note: This prompt is not ready yet. | |
:param user_query: The user's question about financial data. | |
:return: A string containing the BQL query. | |
""" | |
prompt = ( | |
"The user has asked a question related to financial data: '{}'.\n\n" | |
"Assume you are talking to a Bloomberg Chat Server that gives you the data that you request. " | |
"Generate a natural-sounding query to request the data from it to solve the user question. It's " | |
"directly passed through to Bloomberg, so do not provide any explanatory text in the query." | |
).format(user_query) | |
try: | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=100 # Adjust if needed | |
) | |
# Extracting the BQL query from the response | |
bql_query = response.choices[0].text.strip() | |
return bql_query | |
except openai.error.OpenAIError as e: | |
return f"An error occurred: {str(e)}" | |
def chat(self, prompt): | |
# Generate the BQL natural language query from the prompt | |
bql_natural_language_query = self.generate_bql_natural_language_query(prompt) | |
# need a service to map prompt to bql natural language query prompt and then pandas AI prompt | |
# bql_natural_language_query = self.generate_bql_natural_language_query(prompt) | |
bql_natural_language_query = "Return the last 60 days of close prices for B500 Index." | |
bql_query = self.get_bql_function_syntax(bql_natural_language_query) | |
data = bql.combined_df(self.bq.execute(bql_query)).reset_index() | |
# convert to SmartDataframe | |
# Instantiate a SmartDataframe object | |
config = Config(llm=self.llm) | |
pandas_ai = SmartDataframe(data, config=config) | |
return pandas_ai.chat(prompt) | |
if __name__ == "__main__": | |
bgpt = BloombergGPT() | |
user_question = "What tickers are above there 50-day moving average in the B500 Index?" | |
response = bgpt.chat(user_question) | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment