import open_taranis as T
print("program launched\n\n")

# User database
user_database = {
    "jean dupont": {
        "age": 45,
        "position": "Director Marketing",
        "company": "TechCorp",
        "city": "Paris"
    },
    "marie dubois": {
        "age": 32,
        "position": "Software Engineer",
        "company": "TechCorp",
        "city": "Lyon"
    },
    "pierre martin": {
        "age": 29,
        "position": "Data Analyst",
        "company": "DataVision",
        "city": "Marseille"
    },
    "sophie leroy": {
        "age": 38,
        "position": "HR Manager",
        "company": "PeopleFirst",
        "city": "Toulouse"
    },
    "luc bertrand": {
        "age": 50,
        "position": "Chief Financial Officer",
        "company": "FinSecure",
        "city": "Bordeaux"
    },
    "emma moreau": {
        "age": 27,
        "position": "Product Designer",
        "company": "InnovateX",
        "city": "Nantes"
    }
}

# Function implementation - retrieves user data from our database
def get_user_info(name):
    """Fetch user information from the database by name (case-insensitive)"""
    return user_database.get(name.strip().lower(), "User not found in database")

# Tool definition - describes the get_user_info function available to the model
tools = T.functions_to_tools([get_user_info])

client = T.clients.openrouter("API_KEY")
prompt = """Give me the information you have access to on the following users :
- jean dupont
- marie dubois
- pierre martin
- sophie leroy
- luc bertrand
- emma moreau
"""

messages = [
    T.create_system_prompt("You are a technical assistant who MUST use the get_user_info tool before answering questions about users"),
    T.create_user_prompt(prompt)
]

stream = T.clients.openrouter_request(
    client=client,
    messages=messages,
    model="qwen/qwen3-4b:free",
    tools=tools  
)

final_response = ""
print("assistant : ",end="")
for token, tool_calls, _ in T.handle_streaming(stream) : 
    if token :
        print(token, end="")
        final_response += token

print("\n")
print(tool_calls)
print("Number of calls : ",len(tool_calls))
print("="*50,"\n")

messages.append(T.create_assistant_response(final_response, tool_calls))

for tool_call in tool_calls :
    id, name, argument, _ = T.handle_tool_call(tool_call)
    print(id, argument,)
    messages.append(T.create_function_response(
        id, get_user_info(argument["name"]), name
    ))

print("\n","="*50,"\n")

stream = T.clients.openrouter_request(
    client=client,
    messages=messages,
    model="qwen/qwen3-4b:free"
)

final_response = ""
print("assistant final : ",end="")
for token, tool_calls, _ in T.handle_streaming(stream) : 
    if token :
        print(token, end="")
        final_response += token
print()
messages.append(T.create_assistant_response(final_response))

print("\n","="*50,"\n")

for m in messages :
    print(m)
