Ashar086 commited on
Commit
47d5fd4
·
verified ·
1 Parent(s): 1123969

Update openai_agent.py

Browse files
Files changed (1) hide show
  1. 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 a data analyst assistant. Provide insights based on the given data."
9
 
10
  def get_insights(self, df, prompt):
11
- # Prepare the data for the API call
12
- data_sample = df.head(5).to_string()
13
-
14
- user_prompt = f"Here's a sample of my data:\n{data_sample}\n\nBased on this data, {prompt}"
15
-
16
- completion = self.api.chat.completions.create(
17
- model="gpt-4o",
18
- messages=[
19
- {"role": "system", "content": self.system_prompt},
20
- {"role": "user", "content": user_prompt},
21
- ],
22
- temperature=0.7,
23
- max_tokens=256,
24
- )
 
 
 
 
 
 
 
 
25
 
26
- response = completion.choices[0].message.content
27
- return response
 
 
 
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: