paint-brush
Quão boa é a API PaLM do Google?por@raymondcamden
1,484 leituras
1,484 leituras

Quão boa é a API PaLM do Google?

por Raymond Camden10m2023/10/28
Read on Terminal Reader

Muito longo; Para ler

Tipo, presumo que todos os desenvolvedores do planeta tenham sido inundados com GenAI no último ano ou mais.
featured image - Quão boa é a API PaLM do Google?
Raymond Camden HackerNoon profile picture
0-item
1-item

Como todos os outros desenvolvedores do planeta, fui inundado com GenAI no último ano ou assim. Acho isso tão fascinante quanto a maioria das pessoas (pergunte-me sobre quantas vezes tive conversões relacionadas a gatos com o ChatGPT), mas embora seja divertido brincar com ele, também tenho pensado muito sobre os aspectos práticos disso, especialmente via API e integrações. Ainda sou incrivelmente novo em tudo isso e, admito, ainda estou confuso com muitos detalhes, mas pensei em compartilhar algumas impressões iniciais que tive trabalhando com o LLM do Google, PaLM 2 .


Uma olhada na API PaLM do Google



Em primeiro lugar, observe que a API PaLM e o MakerSuite, que é um front-end baseado na web, exigem um login do Google e, pelo menos, a própria API exige solicitação de acesso. Para mim, isso levou menos de vinte e quatro horas e acredito que desta vez seja ainda mais rápido agora, pois o Google está diminuindo a lista de espera. Como eu disse, tive uma aprovação bem rápida e espero que seja o que a maioria das pessoas terá, mas YMMV.


E-mail de boas-vindas do Google sobre acesso à API PaLM e ao MakerSuite


Ao carregar o MakerSuite, você verá um conjunto de prompts diferentes para começar a brincar com a API:


Site MakerSuite mostrando prompts de texto, dados e bate-papo.


Quando cheguei aqui, fui direto para a opção "Prompt de texto", pois imaginei que seria a mais simples e reconhecível das opções. Imediatamente, gostei muito dos exemplos para ajudar a começar:


UI MakerSuite para prompts de texto.


Comecei com este prompt:


 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.


E pegou:


 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.


Isso está praticamente de acordo com o que eu esperaria do ChatGPT e de outros serviços, embora tenha ficado um pouco surpreso com a grafia do gato. Para os curiosos, parece ser um erro ortográfico de Mefistófeles , embora se fosse um trocadilho com “pulgas”, então isso seria muito legal.

O que me atraiu imediatamente foi o botão "Obter código" na parte superior. Clicar aqui traz quatro opções exclusivas:


Amostras de código em cuRL, JavaScript, JSON e Python


Embora estranhamente incluísse a resposta no código, adorei como isso era simples! Aqui está o JavaScript depois de remover manualmente a resposta:


 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)); });


Primeiro, veja como é fácil a autenticação - eu literalmente preciso colar minha chave. Eu usei muitas APIs do Google no passado e, quase sempre, a autenticação é um problema. (Isso provavelmente é totalmente minha culpa!) Além disso, observe a grande utilidade de comentar aqui para o código de exemplo. Serei honesto e direi, mesmo com os comentários, há coisas aqui que não entendi muito bem, mas fora isso, coloquei isso em execução localmente quase imediatamente.


Aqui está o resultado disso:


 [ { "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 ]


Acho o aspecto das classificações de segurança o mais fascinante, pois parece ser uma ótima maneira de adicionar 'proteções' às suas respostas automatizadas.


Depois de brincar com isso, dei uma olhada no recurso "Prompt de dados":


IU de prompt de dados


Pelo que pude perceber, esse recurso permite obter uma lista de amostra de dados e, com base nessa entrada, você pode adicionar linhas adicionais de dados e fazer com que o PaLM especifique a segunda coluna correspondente. Usei o exemplo 'Opostos' porque parecia o mais simples:


Exemplos de dados tabulares para opostos


Executar com sua amostra funciona conforme o esperado:


Com entradas erradas e rápidas, você obtém respostas certas e lentas


Tentei "Dumb", "Cold" e "Left" e obtive:


Dado burro, frio e esquerdo, recebi: Sem conteúdo, quente e certo


Como você pode ver, ele não conseguiu encontrar o oposto de burro, mas funcionou corretamente para os outros. O código para isso, pelo que posso ver, simplesmente 'formata' os dados tabulares em um prompt:


 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)); });


Uma coisa a notar, porém, é que variáveis são permitidas em prompts, o que é muito legal. Quando executei isso, a saída (especificamente o valor output em JSON, estou ignorando o resultado do resultado) foi curta e agradável: "Certo", o que é legal. Meu palpite é que quanto mais complexa e completa for sua tabela, melhor funcionará.


Então... primeiras impressões - estou realmente interessado em quão bem o MakerSuite me permite testar na web e com que rapidez consigo codificar isso e começar a trabalhar. Fiquei tão impressionado que ontem criei uma demonstração "real" com isso em cerca de vinte minutos.


Também publicado aqui .