Last active
July 5, 2024 12:45
-
-
Save delineas/d488b5ce6d18657ebc99bbcb1a03f549 to your computer and use it in GitHub Desktop.
Weather OpenAI function call completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm init -y // inicializa el repo | |
npm i dotenv openai | |
nano .env | |
// En ese fichero tienes que definir estas API KEYS | |
// OPENAI_API_KEY | |
// WEATHER_API_KEY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Configuration, OpenAIApi } from "openai"; | |
import dotenv from "dotenv"; | |
dotenv.config(); | |
const configuration = new Configuration({ | |
apiKey: process.env.OPENAI_API_KEY, | |
}); | |
const openai = new OpenAIApi(configuration); | |
async function getCurrentWeather(location) { | |
const response = await fetch( | |
`https://api.weatherapi.com/v1/current.json?key=${process.env.WEATHER_API_KEY}&q=${location}&aqi=no` | |
); | |
const weatherInfo = await response.json(); | |
return JSON.stringify(weatherInfo); | |
} | |
async function runConversation(question) { | |
let response = await openai.createChatCompletion({ | |
model: "gpt-3.5-turbo-0613", | |
messages: [{ role: "user", content: question }], | |
functions: [ | |
{ | |
name: "getCurrentWeather", | |
description: "Get the current weather in a given location", | |
parameters: { | |
type: "object", | |
properties: { | |
location: { | |
type: "string", | |
description: "The city, eg: Madrid, Barcelona", | |
}, | |
}, | |
required: ["location"], | |
}, | |
}, | |
], | |
function_call: "auto", | |
}); | |
let message = response.data.choices[0].message; | |
console.log(message.function_call); | |
if (message.function_call) { | |
let functionName = message.function_call.name; | |
const parameters = JSON.parse(message.function_call.arguments); | |
let functionResponse = await getCurrentWeather(parameters.location); | |
let secondResponse = await openai.createChatCompletion({ | |
model: "gpt-3.5-turbo-0613", | |
messages: [ | |
{ | |
role: "user", | |
content: ` | |
Responde en castellano en este formato: | |
El pronóstico del tiempo en {location} es {tiempo general} | |
Temperatura: {temperatura en celsius} | |
Sensación de mucho calor: Sí | No | |
Viento: Ninguno | Poco | Mucho | Pelgroso | |
Paraguas: Sí | No | |
`, | |
}, | |
message, | |
{ role: "function", name: functionName, content: functionResponse }, | |
], | |
}); | |
return secondResponse.data.choices[0].message; | |
} | |
} | |
runConversation("¿Cuál es el tiempo en Sevilla?") | |
.then((response) => console.log({ response })) | |
.catch((err) => console.log(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tengo una dura que pasa si yo solo le pido que me responda con un true si entendio la pregunta o si no con 2 preguntas si no entiende lo que el usuario le quiere decir