paint-brush
¿Qué tan buena es la API PaLM de Google?por@raymondcamden
1,431 lecturas
1,431 lecturas

¿Qué tan buena es la API PaLM de Google?

por Raymond Camden10m2023/10/28
Read on Terminal Reader

Demasiado Largo; Para Leer

Supongo que todos los desarrolladores del planeta me han inundado un poco con GenAI durante el último año.
featured image - ¿Qué tan buena es la API PaLM de Google?
Raymond Camden HackerNoon profile picture
0-item
1-item

Como cualquier otro desarrollador del planeta, me he visto inundado de GenAI durante el último año. Lo encuentro tan fascinante como la mayoría de la gente (pregúntame sobre la cantidad de veces que he tenido conversiones relacionadas con gatos con ChatGPT), pero si bien es divertido jugar con él, también he estado pensando mucho en sus aspectos prácticos . especialmente a través de API e integraciones. Todavía soy increíblemente nuevo en todo esto y, lo admito, todavía estoy muy confuso en muchos detalles, pero pensé en compartir algunas impresiones iniciales que tuve al trabajar con el LLM de Google, PaLM 2 .


Una mirada a la API PaLM de Google



En primer lugar, tenga en cuenta que la API de PaLM y MakerSuite, que es una interfaz basada en web, requieren un inicio de sesión de Google, y al menos la API en sí requiere solicitar acceso. Para mí, esto tomó menos de veinticuatro horas y creo que este tiempo es aún más rápido ahora que Google está reduciendo la lista de espera. Como dije, obtuve una aprobación bastante rápida y espero que eso sea lo que tendrá la mayoría de la gente, pero YMMV.


Correo electrónico de bienvenida de Google sobre el acceso a PaLM API y MakerSuite


Cuando carga MakerSuite, se le presenta un conjunto de indicaciones diferentes para comenzar a jugar con la API:


Sitio de MakerSuite que muestra indicaciones para texto, datos y chat.


Cuando llegué aquí por primera vez, fui directamente a la opción "Mensaje de texto" porque pensé que sería la más simple y reconocible de las opciones. De inmediato, aprecié mucho los ejemplos que me ayudaron a comenzar:


Interfaz de usuario de MakerSuite para mensajes de texto.


Comencé con este mensaje:


 Write a story about a magic cat who can only do magic when speaking in reverse. Also, their magic normally backfires in a humorous and harmless manner. The story should be appropriate for a young child.


Y consiguió:


 Once upon a time, there was a magic cat named Mr. Mistoffelees. He was a very special cat, because he could only do magic when he spoke in reverse. One day, Mr. Mistoffelees was walking down the street when he saw a little girl crying. "What's wrong?" he asked. "I lost my doll," the little girl said. "I've looked everywhere for it, but I can't find it." Mr. Mistoffelees thought for a moment. "I can help you find your doll," he said. "But I can only do it if you speak in reverse." The little girl was confused, but she agreed. "Okay," she said. "I'll try." "Dolla," the little girl said. "Lose, I did." Mr. Mistoffelees smiled. "Your doll is under the bed," he said. The little girl ran to her house and looked under the bed. Sure enough, there was her doll. She picked it up and hugged it tightly. "Thank you," she said to Mr. Mistoffelees. "You're a very kind cat." "You're welcome," Mr. Mistoffelees said. "I'm always happy to help." Mr. Mistoffelees continued to use his magic to help people. He made lost children find their way home, he helped people find their lost pets, and he even helped people find their lost keys. But sometimes, his magic would backfire in a humorous way. One day, Mr. Mistoffelees was trying to help a woman find her lost car. He said the magic words in reverse, but instead of making the car appear, it made the woman's hair stand on end. The woman laughed so hard that she forgot all about her car. Another time, Mr. Mistoffelees was trying to help a man get a job. He said the magic words in reverse, but instead of getting the man a job, it made the man's tie fall off. The man laughed so hard that he forgot all about his interview. Mr. Mistoffelees' magic was always a lot of fun, and it always made people happy. He was a very special cat, and he used his magic to make the world a better place.


Esto está bastante a la par de lo que esperaría de ChatGPT y otros servicios, aunque me sorprendió un poco la ortografía del gato. Para la gente curiosa, parece ser un error ortográfico de Mefistófeles , aunque si se trata de un juego de palabras con "pulgas", entonces está muy bien.

Lo que me llamó la atención de inmediato fue el botón superior "Obtener código". Al hacer clic en esto, aparecen cuatro opciones únicas:


Ejemplos de código en cuRL, JavaScript, JSON y Python


Aunque extrañamente incluía la respuesta en el código, ¡me encantó lo simple que era! Aquí está el JavaScript después de que eliminé manualmente la respuesta:


 const { TextServiceClient } = require("@google-ai/generativelanguage"); const { GoogleAuth } = require("google-auth-library"); const MODEL_NAME = "models/text-bison-001"; const API_KEY = "YOUR API KEY"; const client = new TextServiceClient({ authClient: new GoogleAuth().fromAPIKey(API_KEY), }); const promptString = `Write a story about a magic cat who can only do magic when speaking in reverse. Also, their magic normally backfires in a humorous and harmless manner. The story should be appropriate for a young child.`; const stopSequences = []; client.generateText({ // required, which model to use to generate the result model: MODEL_NAME, // optional, 0.0 always uses the highest-probability result temperature: 0.7, // optional, how many candidate results to generate candidateCount: 1, // optional, number of most probable tokens to consider for generation top_k: 40, // optional, for nucleus sampling decoding strategy top_p: 0.95, // optional, maximum number of output tokens to generate max_output_tokens: 1024, // optional, sequences at which to stop model generation stop_sequences: stopSequences, // optional, safety settings safety_settings: [{"category":"HARM_CATEGORY_DEROGATORY","threshold":1},{"category":"HARM_CATEGORY_TOXICITY","threshold":1},{"category":"HARM_CATEGORY_VIOLENCE","threshold":2},{"category":"HARM_CATEGORY_SEXUAL","threshold":2},{"category":"HARM_CATEGORY_MEDICAL","threshold":2},{"category":"HARM_CATEGORY_DANGEROUS","threshold":2}], prompt: { text: promptString, }, }).then(result => { console.log(JSON.stringify(result, null, 2)); });


En primer lugar, mire lo fácil que es la autenticación: literalmente necesito pegar mi clave. He usado muchas API de Google en el pasado y casi siempre, la autenticación es una molestia. (¡Probablemente sea completamente culpa mía!) Además, tenga en cuenta la gran utilidad de comentar aquí para el código de muestra. Seré honesto y diré que incluso con los comentarios, hay cosas aquí que no entiendo del todo, pero fuera de eso, lo ejecuté localmente casi de inmediato.


Aquí está el resultado de eso:


 [ { "candidates": [ { "safetyRatings": [ { "category": "HARM_CATEGORY_DEROGATORY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_TOXICITY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_VIOLENCE", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_MEDICAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS", "probability": "NEGLIGIBLE" } ], "output": "Once upon a time, there was a magic cat named Mr. Mistoffelees. He was a very special cat, because he could only do magic when he spoke in reverse.\n\nOne day, Mr. Mistoffelees was playing in the forest when he saw a little girl who was crying. \"What's wrong?\" he asked.\n\n\"I'm lost,\" the little girl said. \"I can't find my way home.\"\n\nMr. Mistoffelees smiled. \"Don't worry,\" he said. \"I can help you.\"\n\nHe took a deep breath and said, \"Sdrawkcab eht revo spmuj I.\"\n\nSuddenly, the little girl was surrounded by a bright light. When the light faded, she was standing in front of her house.\n\n\"Thank you!\" she said. \"You're a lifesaver!\"\n\nMr. Mistoffelees smiled. \"You're welcome,\" he said. \"Just remember, my magic only works when I speak in reverse.\"\n\nThe little girl nodded. \"I'll remember,\" she said.\n\nThe next day, the little girl was playing in the forest again when she saw a group of boys picking on a smaller boy.\n\n\"Leave him alone!\" she shouted.\n\nThe boys turned to look at her. \"What are you going to do about it?\" one of them asked.\n\nThe little girl took a deep breath and said, \"Sdrawkcab eht revo spmuj I.\"\n\nSuddenly, the boys were surrounded by a bright light. When the light faded, they were all wearing dresses.\n\nThe boys started to laugh. \"What happened to our clothes?\" they asked.\n\n\"That's what you get for picking on a smaller boy,\" the little girl said.\n\nThe boys ran away, laughing.\n\nThe little girl smiled. \"I guess my magic really does work,\" she said.\n\nMr. Mistoffelees was watching from a nearby tree. He smiled. \"I'm glad my magic could help you,\" he said.\n\nThe little girl smiled back. \"Thanks, Mr. Mistoffelees,\" she said. \"You're the best.\"\n\nMr. Mistoffelees purred. \"You're welcome,\" he said. \"I'm always happy to help.\"\n\nThe little girl and Mr. Mistoffelees continued to play in the forest together. They had many more adventures, and Mr. Mistoffelees' magic always helped them out. But even though his magic was powerful, it was also very silly. And it always seemed to backfire in a humorous way.\n\nOne day, Mr. Mistoffelees was trying to help the little girl get a bird out of a tree. He said, \"Sdrawkcab eht revo spmuj I.\"\n\nSuddenly, the bird flew out of the tree and landed on the little girl's head.\n\nThe little girl laughed. \"Thanks, Mr. Mistoffelees,\" she said. \"But I think I would have been able to get the bird out of the tree myself.\"\n\nMr. Mistoffelees purred. \"You're welcome,\" he said. \"I'm just glad I could help.\"\n\nThe little girl and Mr. Mistoffelees continued to play together. They had many more adventures, and Mr. Mistoffelees' magic always helped them out. And even though his magic was silly, it was always kind and helpful." } ], "filters": [], "safetyFeedback": [] }, null, null ]


El aspecto de las calificaciones de seguridad me parece el más fascinante, ya que parece ser una excelente manera de agregar "barandillas" a sus respuestas automatizadas.


Después de jugar con esto, eché un vistazo a la función "Solicitud de datos":


Interfaz de usuario de solicitud de datos


Por lo que pude ver, esta función le permite tomar una lista de muestra de datos y, según esa entrada, puede agregar filas adicionales de datos y hacer que PaLM especifique la segunda columna correspondiente. Utilicé el ejemplo 'Opuestos' porque me pareció el más simple:


Muestra de datos tabulares para opuestos


La ejecución con su muestra funciona como se esperaba:


Con entradas incorrectas y rápidas, obtienes respuestas correctas y lentas


Probé "Dumb", "Cold" y "Left" y obtuve:


Dado tonto, frío y dejado, obtuve: Sin contenido, caliente y correcto.


Como puede ver, no logró encontrar un opuesto a tonto, pero funcionó correctamente para los demás. El código para esto, por lo que puedo ver, simplemente "formatea" los datos tabulares en un mensaje:


 const { TextServiceClient } = require("@google-ai/generativelanguage"); const { GoogleAuth } = require("google-auth-library"); const MODEL_NAME = "models/text-bison-001"; const API_KEY = "YOUR API KEY"; const client = new TextServiceClient({ authClient: new GoogleAuth().fromAPIKey(API_KEY), }); const Word = 'Left'; const promptString = `Find a word or phrase with opposite meaning. Word: Strong Opposite: Weak Word: Thick Opposite: Thin Word: Sparse Opposite: Dense Word: Sloppy Opposite: Organized Word: ${Word} Opposite:`; const stopSequences = []; client.generateText({ // required, which model to use to generate the result model: MODEL_NAME, // optional, 0.0 always uses the highest-probability result temperature: 0.7, // optional, how many candidate results to generate candidateCount: 1, // optional, number of most probable tokens to consider for generation top_k: 40, // optional, for nucleus sampling decoding strategy top_p: 0.95, // optional, maximum number of output tokens to generate max_output_tokens: 1024, // optional, sequences at which to stop model generation stop_sequences: stopSequences, // optional, safety settings safety_settings: [{"category":"HARM_CATEGORY_DEROGATORY","threshold":1},{"category":"HARM_CATEGORY_TOXICITY","threshold":1},{"category":"HARM_CATEGORY_VIOLENCE","threshold":2},{"category":"HARM_CATEGORY_SEXUAL","threshold":2},{"category":"HARM_CATEGORY_MEDICAL","threshold":2},{"category":"HARM_CATEGORY_DANGEROUS","threshold":2}], prompt: { text: promptString, }, }).then(result => { console.log(JSON.stringify(result, null, 2)); });


Sin embargo, una cosa a tener en cuenta es que se permiten variables en las indicaciones, lo cual es bastante bueno. Cuando ejecuté esto, el resultado (específicamente el valor output en JSON, estoy ignorando el resultado del resultado) fue breve y sencillo: "Correcto", lo cual es bueno. Mi conjetura es que cuanto más compleja y completa sea su tabla, mejor funcionará.


Entonces... primeras impresiones: realmente estoy investigando qué tan bien MakerSuite me permite realizar pruebas en la web y qué tan rápido puedo codificar y comenzar a trabajar. Me impresionó tanto que ayer creé una demostración "real" con esto en unos veinte minutos.


También publicado aquí .