-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpreter.py
More file actions
28 lines (23 loc) · 945 Bytes
/
interpreter.py
File metadata and controls
28 lines (23 loc) · 945 Bytes
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
import openai
import streamlit as st
client = openai.OpenAI(api_key=st.secrets["openai"]["OPENAI_API_KEY"])
def interpret_prompt(prompt, url):
try:
system_message = (
"You're a web scraping assistant. Convert the user prompt into a CSS selector "
"or scraping rule that can be used to extract data from the given webpage."
)
user_message = f"URL: {url}\nInstruction: {prompt}"
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": user_message}
],
temperature=0.2,
)
selector = response.choices[0].message.content.strip()
return selector
except Exception as e:
print(f"Error in AI interpretation: {e}")
return "body" # fallback selector