Update openai_agent.py
Browse files- openai_agent.py +37 -17
openai_agent.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
|
| 3 |
class OpenAIAgent:
|
|
@@ -5,27 +6,46 @@ class OpenAIAgent:
|
|
| 5 |
self.base_url = "https://api.aimlapi.com/v1"
|
| 6 |
self.api_key = "c496d9094ba54ddb9d66eeeb35a6196f" # Replace with your actual API key
|
| 7 |
self.api = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
| 8 |
-
self.system_prompt = "You are
|
| 9 |
|
| 10 |
def get_insights(self, df, prompt):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
'''import openai
|
| 30 |
|
| 31 |
class OpenAIAgent:
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
class OpenAIAgent:
|
|
|
|
| 6 |
self.base_url = "https://api.aimlapi.com/v1"
|
| 7 |
self.api_key = "c496d9094ba54ddb9d66eeeb35a6196f" # Replace with your actual API key
|
| 8 |
self.api = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
| 9 |
+
self.system_prompt = "You are an AI assistant specialized in data analysis, visualization, and insights generation. Provide clear and concise responses."
|
| 10 |
|
| 11 |
def get_insights(self, df, prompt):
|
| 12 |
+
try:
|
| 13 |
+
data_sample = df.head(5).to_string()
|
| 14 |
+
data_info = f"Dataset shape: {df.shape}\nColumns: {', '.join(df.columns)}\n"
|
| 15 |
+
full_prompt = f"{data_info}\n{data_sample}\n\n{prompt}"
|
| 16 |
+
|
| 17 |
+
completion = self.api.chat.completions.create(
|
| 18 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 19 |
+
messages=[
|
| 20 |
+
{"role": "system", "content": self.system_prompt},
|
| 21 |
+
{"role": "user", "content": full_prompt},
|
| 22 |
+
],
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
max_tokens=500,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
return completion.choices[0].message.content
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"An error occurred while getting insights: {str(e)}"
|
| 30 |
+
|
| 31 |
+
def generate_insights(self, df):
|
| 32 |
+
insight_type = st.selectbox("Select insight type",
|
| 33 |
+
["General Insights", "Trend Analysis", "Anomaly Detection", "Predictive Analysis"])
|
| 34 |
|
| 35 |
+
if st.button("Generate Insights"):
|
| 36 |
+
with st.spinner("Generating insights..."):
|
| 37 |
+
prompt = f"Analyze the following dataset and provide {insight_type}."
|
| 38 |
+
ai_response = self.get_insights(df, prompt)
|
| 39 |
+
st.write(ai_response)
|
| 40 |
|
| 41 |
+
def custom_ai_task(self, df):
|
| 42 |
+
custom_task = st.text_area("Describe your custom task:")
|
| 43 |
+
|
| 44 |
+
if st.button("Execute Custom Task"):
|
| 45 |
+
with st.spinner("Processing custom task..."):
|
| 46 |
+
prompt = f"Perform the following task on the given dataset:\n{custom_task}"
|
| 47 |
+
ai_response = self.get_insights(df, prompt)
|
| 48 |
+
st.write(ai_response)
|
| 49 |
'''import openai
|
| 50 |
|
| 51 |
class OpenAIAgent:
|