-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
49 lines (38 loc) · 1.43 KB
/
utils.py
File metadata and controls
49 lines (38 loc) · 1.43 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import requests
from bs4 import BeautifulSoup
from openai import OpenAI
def extract_contents_from_url(source_url):
response = requests.get(source_url)
if response.status_code != 200:
return None
else:
soup = BeautifulSoup(response.content, 'html.parser')
text_content = soup.get_text()
return text_content
class GPTWrapper:
def __init__(self, model_name=None, response_format=None, system_prompt=None, seed=None, temperature=None):
self.client = OpenAI()
if model_name is None:
model_name = "gpt-4-1106-preview"
self.model_name = model_name
if system_prompt is None:
system_prompt = "You are a helpful assistant."
self.system_prompt = system_prompt
if response_format == 'json':
response_format = {"type": 'json_object'}
self.response_format = response_format
self.seed = seed
self.temperature = temperature
def __call__(self, query):
completion = self.client.chat.completions.create(
model=self.model_name,
response_format=self.response_format,
seed=self.seed,
temperature=self.temperature,
messages=[
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": query},
]
)
return completion.choices[0].message.content