paint-brush
API PaLM của Google tốt đến mức nào?từ tác giả@raymondcamden
1,407 lượt đọc
1,407 lượt đọc

API PaLM của Google tốt đến mức nào?

từ tác giả Raymond Camden10m2023/10/28
Read on Terminal Reader

dài quá đọc không nổi

Tôi cho rằng, giống như mọi nhà phát triển trên hành tinh, tôi đã phần nào bị choáng ngợp bởi GenAI trong khoảng một năm qua.
featured image - API PaLM của Google tốt đến mức nào?
Raymond Camden HackerNoon profile picture
0-item
1-item

Giống như mọi nhà phát triển khác trên hành tinh, tôi đã phần nào bị choáng ngợp bởi GenAI trong khoảng một năm qua. Tôi thấy nó thú vị như hầu hết mọi người (hãy hỏi tôi về số lần tôi đã nhận được chuyển đổi liên quan đến mèo với ChatGPT) nhưng mặc dù chơi cùng nó rất thú vị nhưng tôi cũng đã suy nghĩ rất nhiều về các khía cạnh thực tế của nó, đặc biệt là thông qua API và tích hợp. Tôi vẫn còn rất mới đối với tất cả những điều này và tôi phải thừa nhận rằng vẫn còn khá mơ hồ về nhiều chi tiết, nhưng tôi nghĩ tôi sẽ chia sẻ một số ấn tượng ban đầu khi làm việc với LLM của Google, PaLM 2 .


Nhìn vào API PaLM của Google



Đầu tiên và quan trọng nhất, hãy lưu ý rằng API PaLM và MakerSuite, một giao diện người dùng dựa trên web, yêu cầu đăng nhập vào Google và ít nhất bản thân API cũng yêu cầu quyền truy cập. Đối với tôi, việc này chỉ mất chưa tới 24 giờ và tôi tin rằng thời gian này thậm chí còn nhanh hơn vì Google đang thu hẹp danh sách chờ. Như tôi đã nói, tôi đã nhận được sự chấp thuận khá nhanh chóng và tôi hy vọng đó là điều mà hầu hết mọi người sẽ có, ngoại trừ YMMV.


Email chào mừng từ Google liên quan đến quyền truy cập vào API PaLM và MakerSuite


Khi tải MakerSuite lên, bạn sẽ thấy một loạt lời nhắc khác nhau để bắt đầu sử dụng API:


Trang web MakerSuite hiển thị lời nhắc về văn bản, dữ liệu và trò chuyện.


Khi tôi mới đến đây, tôi đã đi thẳng vào tùy chọn "Lời nhắc văn bản" vì tôi nghĩ rằng đó là tùy chọn đơn giản và dễ nhận biết nhất. Ngay lập tức, tôi thực sự đánh giá cao các mẫu có thể giúp bạn bắt đầu:


Giao diện người dùng MakerSuite cho lời nhắc văn bản.


Tôi bắt đầu với lời nhắc này:


 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.


Và có:


 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.


Điều này khá giống với những gì tôi mong đợi từ ChatGPT và các dịch vụ khác, mặc dù tôi hơi ngạc nhiên về cách viết của con mèo. Đối với những người tò mò, nó có vẻ là một lỗi chính tả của Mephistopheles , mặc dù nếu nó là một cách chơi chữ của "bọ chét" thì điều đó khá tuyệt.

Điều tôi chú ý ngay lập tức là nút "Nhận mã" ở trên cùng. Nhấp vào đây sẽ xuất hiện bốn tùy chọn duy nhất:


Mẫu mã bằng cuRL, JavaScript, JSON và Python


Mặc dù nó bao gồm phản hồi trong mã một cách kỳ lạ nhưng tôi thực sự thích sự đơn giản của nó! Đây là JavaScript sau khi tôi xóa phản hồi theo cách thủ công:


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


Trước hết, hãy xem việc xác thực dễ dàng như thế nào - tôi thực sự cần phải dán khóa của mình vào. Trước đây, tôi đã sử dụng rất nhiều API của Google và gần như mọi lúc, việc xác thực đều là một vấn đề khó khăn. (Đó có thể hoàn toàn là lỗi của tôi!) Ngoài ra, hãy lưu ý công dụng tuyệt vời của nhận xét ở đây đối với mã mẫu. Tôi sẽ thành thật mà nói ngay cả khi có nhận xét, ở đây có những thứ tôi không hiểu lắm, nhưng ngoài điều đó ra, tôi đã chạy nó cục bộ gần như ngay lập tức.


Đây là kết quả của việc đó:


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


Tôi nhận thấy khía cạnh xếp hạng an toàn là hấp dẫn nhất vì đây có vẻ là một cách tuyệt vời để thêm 'lan can' vào các phản hồi tự động của bạn.


Sau khi chơi xong, tôi xem xét tính năng "Dấu nhắc dữ liệu":


Giao diện người dùng nhắc dữ liệu


Theo những gì tôi có thể nói, tính năng này cho phép bạn lấy danh sách dữ liệu mẫu và dựa trên thông tin đầu vào đó, sau đó bạn có thể thêm các hàng dữ liệu bổ sung và yêu cầu PaLM chỉ định cột thứ hai tương ứng. Tôi đã sử dụng mẫu 'Đối lập' vì nó có vẻ đơn giản nhất:


Dữ liệu dạng bảng mẫu cho các từ đối lập


Chạy với mẫu của họ hoạt động như mong đợi:


Với đầu vào sai và nhanh, bạn sẽ nhận được phản hồi đúng và chậm


Tôi đã thử "Ngốc", "Lạnh" và "Trái" và nhận được:


Bị câm, lạnh và trái, tôi nhận được: Không có nội dung, nóng và phải


Như bạn có thể thấy, nó không tìm được từ đối lập với từ ngu ngốc nhưng lại hoạt động chính xác với những từ khác. Mã cho điều này, từ những gì tôi có thể thấy, chỉ đơn giản là 'định dạng' dữ liệu dạng bảng theo lời nhắc:


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


Tuy nhiên, một điều cần lưu ý là các biến được phép trong lời nhắc, điều này khá thú vị. Khi tôi chạy cái này, đầu ra (cụ thể là giá trị output trong JSON, tôi bỏ qua kết quả của kết quả đó) rất ngắn và hấp dẫn: "Đúng", điều này thật tuyệt. Tôi đoán là bảng của bạn càng phức tạp và đầy đủ thì bảng này sẽ hoạt động càng tốt.


Vì vậy... ấn tượng đầu tiên - Tôi thực sự đang tìm hiểu xem MakerSuite cho phép tôi thử nghiệm trên web tốt như thế nào và tôi có thể áp dụng điều đó vào mã và bắt đầu làm việc nhanh như thế nào. Tôi rất ấn tượng nên hôm qua tôi đã tạo được một bản demo "thật" bằng sản phẩm này trong khoảng 20 phút.


Cũng được xuất bản ở đây .