-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_engine.py
55 lines (47 loc) · 1.41 KB
/
compute_engine.py
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
53
54
55
import os
from dotenv import load_dotenv
import anthropic
import json
# Load environment variables from .env file
def extract_json_from_text(text):
# Function to find and extract JSON-like structures from text
start_index = text.find('{')
end_index = text.rfind('}') + 1
if start_index != -1 and end_index != -1:
json_str = text[start_index:end_index]
try:
json_data = json.loads(json_str)
return json_data
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return None
else:
print("No JSON-like structure found in the text.")
return None
def read_text(text_file):
f = open(text_file,'r')
return f.read()
def generate_json(obj):
prompt = read_text('data/prompt.txt')
load_dotenv()
ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1000,
temperature=0,
system=prompt,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": obj
}
]
}
]
).content[0].text
json_data = extract_json_from_text(message)
return json_data