File size: 2,263 Bytes
308a6e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
import os
import re
from openai import OpenAI

# 1. Config (Required by OpenEnv checklist)
API_BASE_URL = os.getenv("API_BASE_URL", "http://127.0.0.1:7860")
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4") # Or whichever model you are using
HF_TOKEN = os.getenv("HF_TOKEN", "")

# 2. Setup the Client
# Note: We point base_url to the environment itself if testing locally, 
# but the script uses OpenAI client for the LLM logic as requested.
client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN)

def get_action_from_llm(task_description, observation):
    prompt = f"""
    You are a SQL Data Janitor. Your goal is: {task_description}
    Current Schema: {observation.get('schema_info')}
    Last Query Results: {observation.get('query_result')}
    Last Error: {observation.get('error_message')}
    
    Provide EXACTLY one SQL query to help solve the task. 
    Respond only with the SQL query string.
    """
    
    response = client.chat.completions.create(
        model=MODEL_NAME,
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content.strip()

def main():
    # We use httpx to talk to our own environment endpoints
    import httpx
    
    tasks = ["task_1_null_fixer", "task_2_price_standardizer", "task_3_duplicate_merger"]
    
    for task_id in tasks:
        print(f"\n--- Starting Task: {task_id} ---")
        
        # Reset Environment for specific task
        res = httpx.post(f"{API_BASE_URL}/reset?task_id={task_id}")
        obs = res.json()["observation"]
        
        for step in range(5): # Allow up to 5 attempts per task
            sql_query = get_action_from_llm(task_id, obs)
            print(f"Step {step+1} | AI Query: {sql_query}")
            
            # Send action to environment
            step_res = httpx.post(f"{API_BASE_URL}/step", json={"query": sql_query})
            data = step_res.json()
            
            obs = data["observation"]
            reward = data["reward"]
            done = data["done"]
            
            print(f"Reward: {reward} | Done: {done}")
            
            if done:
                print(f"SUCCESS: {task_id} completed!")
                break

if __name__ == "__main__":
    main()