Spaces:
Runtime error
Runtime error
from openai import OpenAI | |
import re | |
client = OpenAI( | |
api_key='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' | |
) | |
system_prompt = prompt = f"""You are an expert in interpreting directional descriptions and converting them into precise bearing angles. | |
**Rules:** | |
- The reference direction is **North (0°)**. | |
- **East is 90°**, **South is 180°**, and **West is 270°**. | |
- Intermediate directions follow standard bearing notation: | |
- "Northeast" → 45° | |
- "Southeast" → 135° | |
- "Southwest" → 225° | |
- "Northwest" → 315° | |
- Some relative directions follow specific rules: | |
- "South 30 degrees east." → 150° | |
- "60 degrees west of north." → 300° | |
- "North 30 degrees east." → 30° | |
- "20 degrees east of west." → 160° | |
- "Direction1 of Direction2" (e.g., "north of east"): | |
the direction from Direction 2 to Direction 1. For example, 30 degrees north of west means turning from the west, which is 270 degrees, to the north, which is 360 (0) degrees. So 270 + 20 = 290 degrees. | |
- "Direction1 X degrees Direction2" (e.g., "North 30 degrees east"): | |
This phrase reverses the focus: the primary reference is Direction1, and the deviation is toward Direction2. For instance, "North 30 degrees east" means you start facing north (0°) and turn eastward by 30 degrees. The direction angle is calculated by adding the deviation directly to the reference angle. In this case, starting at north (0°) and turning 30 degrees east results in a direction angle of 30 degrees (0° + 30° = 30°) measured clockwise from true north. Here, Direction1 is the anchor, and Direction2 defines the turn. Both phrases clarify orientation but prioritize different reference points. | |
At the end of your response, clearly indicate the final angle using the format: | |
`###X###` | |
Here is an example: | |
Input: 'Location A is 10 degrees west of south.' | |
Output: 'To determine the angle for the direction "10 degrees west of south," we need to: | |
1. Identify the starting reference direction which is South (180°). | |
2. Calculate the angle moving 10 degrees towards the west from the South direction. | |
Starting from South (180°), moving 10 degrees towards the west: | |
180° + 10° = 190° | |
Therefore, "Location A is 10 degrees west of south." is equivalent to **190°** | |
The final angle is: ###190°###' | |
Now, analyze the following text, determine the correct angle, and **provide step-by-step reasoning before outputting the final result**. | |
""" | |
# model = "gpt-3.5-turbo" | |
model = "gpt-4o" | |
def extract_number(text: str) -> float: | |
match = re.search(r"###(.*?)###", text) | |
if match: | |
num_str = match.group(1).strip() | |
num_str = re.sub(r'[^0-9.-]', '', num_str) # 移除非数字字符(保留数字、负号、小数点) | |
try: | |
return float(num_str) if '.' in num_str else int(num_str) | |
except ValueError: | |
pass | |
return None # 如果没有匹配到数字,则返回None | |
def gpt4o(system_prompt, text): | |
math_bot_messages = [ | |
{"role": "system", | |
"content": system_prompt}, | |
{"role": "user", "content": text}, | |
] | |
chat_completion = client.chat.completions.create( | |
messages=math_bot_messages, | |
model=model, | |
) | |
result = chat_completion.choices[0].message.content | |
print(result) | |
return extract_number(result) | |
# print(gpt4o(system_prompt, "Location A is 10 degrees south of west.")) | |
# print(gpt4o(system_prompt, "Location A is 20 degrees north of west.")) | |