Files
semantic-kernel/python/samples/kernel-syntax-examples/google_palm_text_completion.py
T
b029e50b6d Python: Add Google PaLM connector with text completion and example file (#2076)
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Implementation of Google PaLM connector with text completion and an
example file to demonstrate its functionality.
Closes #1979

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

1. Implemented Google Palm connector with text completion 
2. Added example file to ```python/samples/kernel-syntax-examples```
3. Added integration tests with different inputs to kernel.run_async
4. Added unit tests to ensure successful initialization of the class and
successful API calls
5. 3 optional arguments (top_k, safety_settings, client) for
google.generativeai.generate_text were not included. See more
information about the function and its arguments:
https://developers.generativeai.google/api/python/google/generativeai/generate_text

I also opened a PR for text embedding and chat completion #2258

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#dev-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

Currently no warnings, there was 1 warning when first installing genai
with `poetry add google.generativeai==v0.1.0rc2` from within poetry
shell: "The locked version 0.1.0rc2 for google-generativeai is a yanked
version. Reason for being yanked: Release is marked as supporting Py3.8,
but in practice it requires 3.9". We would need to require later
versions of python to fix it.

---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
2023-08-17 21:56:26 +00:00

55 lines
1.9 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import asyncio
import semantic_kernel as sk
import semantic_kernel.connectors.ai.google_palm as sk_gp
from semantic_kernel.connectors.ai.complete_request_settings import (
CompleteRequestSettings,
)
async def text_completion_example_complete_async(kernel, api_key, user_mssg, settings):
"""
Complete a text prompt using the Google PaLM model and print the results.
"""
palm_text_completion = sk_gp.GooglePalmTextCompletion(
"models/text-bison-001", api_key
)
kernel.add_text_completion_service("models/text-bison-001", palm_text_completion)
answer = await palm_text_completion.complete_async(user_mssg, settings)
return answer
async def main() -> None:
kernel = sk.Kernel()
apikey = sk.google_palm_settings_from_dot_env()
settings = CompleteRequestSettings()
user_mssg1 = (
"Sam has three boxes, each containing a certain number of coins. "
"The first box has twice as many coins as the second box, and the second "
"box has three times as many coins as the third box. Together, the three "
"boxes have 98 coins in total. How many coins are there in each box? "
"Think about it step by step, and show your work."
)
response = await text_completion_example_complete_async(
kernel, apikey, user_mssg1, settings
)
print(f"User:> {user_mssg1}\n\nChatBot:> {response}\n")
# Use temperature to influence the variance of the responses
settings.number_of_responses = 3
settings.temperature = 1
user_mssg2 = (
"I need a concise answer. A common method for traversing a binary tree is"
)
response = await text_completion_example_complete_async(
kernel, apikey, user_mssg2, settings
)
print(f"User:> {user_mssg2}\n\nChatBot:> {response}")
return
if __name__ == "__main__":
asyncio.run(main())