-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomGenerator.py
More file actions
52 lines (43 loc) · 1.68 KB
/
CustomGenerator.py
File metadata and controls
52 lines (43 loc) · 1.68 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
50
51
52
from target_benchmark.generators import AbsGenerator
from openai import AzureOpenAI
import os
from dotenv import load_dotenv
class CustomGenerator(AbsGenerator):
def __init__(self):
# Set the current working directory to the directory of this script
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Load environment file for secrets
try:
if not load_dotenv('.env'):
raise TypeError
except TypeError:
print('Unable to load .env file.')
quit()
# Initialize the AzureOpenAI client
self.client = AzureOpenAI(
api_key=os.environ['OPENAI_API_KEY'],
api_version=os.environ['API_VERSION'],
azure_endpoint=os.environ['OPENAI_API_BASE'],
organization="016732"
)
self.model = "gpt-4o"
print(f"Initializing generator with model: {self.model}")
# Generate the answer to the query
def generate(self, table_str: str, query: str) -> str:
# Combine the table string and query to form the input for the model
input_text = f"Table: {table_str}\nQuestion: {query}\nAnswer:"
print(f"Generating answer for input: {input_text}")
messages = [
{"role": "system", "content": "You are a QA chatbot"},
{"role": "user", "content": input_text},
]
# Use the loaded generator model
outputs = self.client.chat.completions.create(
model=self.model,
messages=messages,
temperature=0,
stop=None
)
# Extract the answer from the response
answer = outputs.choices[0].message.content
return answer