-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
gp_text_completion.py
94 lines (79 loc) · 3.73 KB
/
gp_text_completion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (c) Microsoft. All rights reserved.
import logging
import sys
from typing import List
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated
import google.generativeai as palm
from google.generativeai.types import Completion
from google.generativeai.types.text_types import TextCompletion
from pydantic import StringConstraints
from semantic_kernel.connectors.ai.google_palm.gp_prompt_execution_settings import GooglePalmTextPromptExecutionSettings
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.connectors.ai.text_completion_client_base import TextCompletionClientBase
from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.exceptions import ServiceResponseException
logger: logging.Logger = logging.getLogger(__name__)
class GooglePalmTextCompletion(TextCompletionClientBase):
api_key: Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]
def __init__(self, ai_model_id: str, api_key: str):
"""
Initializes a new instance of the GooglePalmTextCompletion class.
Arguments:
ai_model_id {str} -- GooglePalm model name, see
https://developers.generativeai.google/models/language
api_key {str} -- GooglePalm API key, see
https://developers.generativeai.google/products/palm
"""
super().__init__(ai_model_id=ai_model_id, api_key=api_key)
async def complete(self, prompt: str, settings: GooglePalmTextPromptExecutionSettings) -> List[TextContent]:
"""
This is the method that is called from the kernel to get a response from a text-optimized LLM.
Arguments:
prompt {str} -- The prompt to send to the LLM.
settings {GooglePalmTextPromptExecutionSettings} -- Settings for the request.
Returns:
List[TextContent] -- A list of TextContent objects representing the response(s) from the LLM.
"""
settings.prompt = prompt
if not settings.ai_model_id:
settings.ai_model_id = self.ai_model_id
try:
palm.configure(api_key=self.api_key)
except Exception as ex:
raise PermissionError(
"Google PaLM service failed to configure. Invalid API key provided.",
ex,
)
try:
response = palm.generate_text(**settings.prepare_settings_dict())
except Exception as ex:
raise ServiceResponseException(
"Google PaLM service failed to complete the prompt",
ex,
) from ex
return [self._create_text_content(response, candidate) for candidate in response.candidates]
def _create_text_content(self, response: Completion, candidate: TextCompletion) -> TextContent:
"""Create a text content object from a candidate."""
return TextContent(
inner_content=response,
ai_model_id=self.ai_model_id,
text=candidate.get("output"),
metadata={
"filters": response.filters,
"safety_feedback": response.safety_feedback,
"citation_metadata": candidate.get("citation_metadata"),
"safety_ratings": candidate.get("safety_ratings"),
},
)
async def complete_stream(
self,
prompt: str,
settings: GooglePalmTextPromptExecutionSettings,
):
raise NotImplementedError("Google Palm API does not currently support streaming")
def get_prompt_execution_settings_class(self) -> "PromptExecutionSettings":
"""Create a request settings object."""
return GooglePalmTextPromptExecutionSettings