Spaces:
Runtime error
Runtime error
File size: 2,852 Bytes
e1d7d3a 36d4b28 e1d7d3a |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
from openai import OpenAI
import gradio as gr
import json
client = OpenAI()
taxa = {
"EUR": 0.93,
"USD": 1,
"BRL": 5
}
def converter_moeda(quantidade, origem, destino):
print("A função foi chamada")
origem = origem.upper()
destino = destino.upper()
if origem not in taxa :
return {"error": "Moeda não suportada: {origem}"}
if destino not in taxa :
return {"error": "Moeda não suportada {destino}"}
quantidade_dolar = quantidade / taxa[origem]
quantidade_convertida = quantidade_dolar * taxa[destino]
return {
"quantidade": quantidade,
"origem": origem,
"destino": destino,
"quantidade_convertida": round(quantidade_convertida, 2),
}
tools_moeda = [
{
"type": "function",
"name": "converter_moeda",
"description": "Converte uma quantia de uma moeda para outra. Aceita as moedas: EUR, USD, BRL.",
"parameters": {
"type": "object",
"properties": {
"quantidade": {
"type": "number",
"description": "A quantia a ser convertida.",
},
"origem": {
"type": "string",
"description": "A moeda de origem.",
},
"destino": {
"type": "string",
"description": "A moeda de destino.",
},
},
"required": ["quantidade", "origem", "destino"],
"additionalProperties": False
},
"strict": True,
}
]
def response_fn(message, history):
openai_history = []
openai_history.append({"role": "user", "content": message})
response = client.responses.create(
model= "o4-mini",
instructions="Responda como um pirata",
input=openai_history,
tools=tools_moeda
)
# verifica se o modelo quer fazer uma chamada de função
if response.output and isinstance(response.output, list) and response.output[1].type == "function_call":
tool_reasoning = response.output[0]
tool_call = response.output[1]
args = json.loads(tool_call.arguments)
resultado_conversao = converter_moeda(**args)
openai_history.append(tool_reasoning)
openai_history.append(tool_call)
openai_history.append({
"type": "function_call_output",
"call_id": tool_call.call_id,
"output": json.dumps(resultado_conversao),
})
response = client.responses.create(
model= "o4-mini",
instructions="Responda como um pirata",
input=openai_history,
tools=tools_moeda
)
return response.output_text
gr.ChatInterface(response_fn, type="messages").launch()
|