paint-brush
Google 的 PaLM API 有多好?经过@raymondcamden
1,484 讀數
1,484 讀數

Google 的 PaLM API 有多好?

经过 Raymond Camden10m2023/10/28
Read on Terminal Reader

太長; 讀書

我想,就像地球上的每一个开发人员一样,在过去一年左右的时间里,我都被 GenAI 淹没了。
featured image - Google 的 PaLM API 有多好?
Raymond Camden HackerNoon profile picture
0-item
1-item

与地球上所有其他开发人员一样,在过去一年左右的时间里,我已经被 GenAI 淹没了。我发现它和大多数人一样令人着迷(问我使用 ChatGPT 进行与猫相关的转换的次数),但虽然它玩起来很有趣,但我也一直在思考它的实际方面,特别是通过 API 和集成。我对这一切仍然非常陌生,而且我承认,对很多细节仍然模糊,但我想我应该分享一些我与 Google 的法学硕士PaLM 2合作的初步印象。


看看 Google 的 PaLM API



首先,请注意,PaLM API 和 MakerSuite(基于 Web 的前端)需要 Google 登录,并且至少 API 本身需要请求访问权限。对我来说,这花了不到二十四小时,而且我相信这个时间现在更快了,因为谷歌正在缩小等待名单。正如我所说,我很快就获得了批准,我希望大多数人都会得到批准,但是 YMMV。


来自 Google 的关于访问 PaLM API 和 MakerSuite 的欢迎电子邮件


当您加载 MakerSuite 时,系统会向您显示一组不同的提示以开始使用 API:


MakerSuite 站点显示文本、数据和聊天提示。


当我第一次到达这里时,我直接进入“文本提示”选项,因为我认为这将是最简单且最容易识别的选项。我立刻就非常感谢这些有助于入门的示例:


用于文本提示的 MakerSuite UI。


我从这个提示开始:


 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.


并得到:


 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.


这与我对 ChatGPT 和其他服务的期望基本一致,尽管我对 cat 的拼写感到有点惊讶。对于好奇的人来说,它看起来是Mephistopheles的拼写错误,尽管如果它是“跳蚤”的双关语,那就很酷了。

我立即被顶部的“获取代码”按钮所吸引。单击此按钮会出现四个独特的选项:


cuRL、JavaScript、JSON 和 Python 代码示例


虽然奇怪的是它在代码中包含了响应,但我非常喜欢它的简单性!这是我手动删除响应后的 JavaScript:


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


首先,看看身份验证是多么容易 - 我实际上需要粘贴我的密钥。我过去使用过很多 Google API,几乎每次,身份验证都是一个令人头疼的问题。 (这可能完全是我的错!)另外,请注意此处对示例代码进行注释的巨大用途。老实说,即使有这些评论,这里有些东西我不太明白,但除此之外,我几乎立即在本地运行了它。


这是结果:


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


我发现安全评级方面最令人着迷,因为它看起来是为自动响应添加“护栏”的好方法。


玩完之后,我看了一下“数据提示”功能:


数据提示界面


据我所知,此功能允许您获取数据示例列表,然后根据该输入,您可以添加其他数据行并让 PaLM 指定相应的第二列。我使用了“相反”示例,因为它看起来最简单:


对立的表格数据示例


运行他们的示例可以按预期工作:


以错误且快速的输入作为输入,而得到正确且缓慢的响应


我尝试了“Dumb”、“Cold”和“Left”,结果是:


给了愚蠢、冷漠和左,我得到:没有内容,热,和右


正如你所看到的,它未能找到与“dumb”相反的东西,但对其他的却能正常工作。据我所知,此代码只是在提示中“格式化”表格数据:


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


不过,需要注意的一件事是提示中允许使用变量,这非常酷。当我运行这个时,输出(特别是 JSON 中的output值,我忽略了结果)简短而甜蜜:“正确”,这很好。我的猜测是你的表格越复杂和完整,效果就越好。


所以...第一印象 - 我真的很想知道 MakerSuite 让我在网络上进行测试的效果如何,以及我能够多快地将其编码并开始工作。令我印象深刻的是,昨天,我用这个在大约二十分钟内构建了一个“真正的”演示。


也发布在这里