Ashar086 commited on
Commit
8ce9f55
·
verified ·
1 Parent(s): 5b2aaec

Update openai_agent.py

Browse files
Files changed (1) hide show
  1. openai_agent.py +31 -2
openai_agent.py CHANGED
@@ -1,4 +1,32 @@
1
- import openai
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  class OpenAIAgent:
4
  def __init__(self):
@@ -20,4 +48,5 @@ class OpenAIAgent:
20
  max_tokens=150
21
  )
22
 
23
- return response.choices[0].message['content']
 
 
1
+ from openai import OpenAI
2
+
3
+ class OpenAIAgent:
4
+ def __init__(self):
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:
32
  def __init__(self):
 
48
  max_tokens=150
49
  )
50
 
51
+ return response.choices[0].message['content']
52
+ '''