14May
Index
ToggleOpenAI has recently launched the new ChatGPT GPT-4o, an advanced version of its language model that significantly improves text comprehension and generation.
This update offers greater precision in linguistic tasks such as text translation.
In this blog post, We'll show you how you can take advantage of this amazing technology to create a function in Excel using VBA to automatically translate text.
Specifically, Let's learn how to create a function in Excel using VBA to automatically translate text.
This function will use the OpenAI API with the GPT-4O model to detect the language of the text and translate it to the language of our choice.
In the YouTube video, explained step by step:
Function Traducira(texto As String, idiomaDestino As String) As String
Dim apiKey As String
'Dim idiomaDestino As String
apiKey = "--------------AQUÍ TIENES QUE PONER TU NÚMERO DE API----------------"
Dim http As Object
Dim url As String
Dim postData As String
Dim respuesta As String
Dim json As Object
' Construye la URL de la API de OpenAI
url = "https://api.openai.com/v1/chat/completions"
' Prepara los datos de la solicitud en formato JSON
postData = "{""model"": ""gpt-4o"",""messages"": [{""role"": ""system"", ""content"": ""Eres un asistente útil que traduce texto. pero solo devuelve la tracuccion""}, {""role"": ""user"", ""content"": ""Translate the following text to " & idiomaDestino & ": " & texto & """}], ""max_tokens"": 100}"
' Crea el objeto HTTP
Set http = CreateObject("MSXML2.XMLHTTP")
' Envía la solicitud a la API
With http
.Open "POST", url, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & apiKey
.send postData
End With
' Obtiene la respuesta
respuesta = http.responseText
' Extrae la traducción de la respuesta JSON
Set json = JsonConverter.ParseJson(respuesta)
' Maneja el caso de error en la respuesta
On Error Resume Next
Traducira = Trim(json("choices")(1)("message")("content"))
On Error GoTo 0
' Limpia
Set http = Nothing
Set json = Nothing
End Function
These are the fantastic results of programming a macro that integrates the OPENAI API with Microsoft Excel.
In order to deserialize the HTTP request to OpenAi in Excel we will need to include the JsonConverter.bas module that we can download from GitHub, from the following link:
https://github.com/VBA-tools/VBA-JSON/blob/master/JsonConverter.bas
Another article that may be interesting to you related to the use of artificial intelligence is this one about…
Have you ever wanted to see your messages and emails without removing the.... read more
A few days ago the Apple developers conference was held,... read more
Have you ever felt nervous getting on a plane?? No estás... read more
Breaking Bad, Considered by many as one of the best series of.... read more
Leave a Reply