VertexAICodeGenerator
此组件支持使用 Google Vertex AI 生成模型生成代码。
| 强制运行变量 | “prefix”:当前点之前的代码字符串 “suffix”:当前点之后的可选代码字符串 |
| 输出变量 | “replies”:模型生成的代码 |
| API 参考 | Google Vertex |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex |
VertexAICodeGenerator 支持code-bison, code-bison-32k,以及code-gecko.
参数概述
VertexAICodeGenerator 使用 Google Cloud 应用程序默认凭据(ADC)进行身份验证。有关如何设置 ADC 的更多信息,请参阅官方文档。
请记住,使用有权访问已授权使用 Google Vertex AI 端点的项目的帐户至关重要。
您可以在GCP 资源管理器中找到您的项目 ID,或者通过在终端中运行gcloud projects list 来在本地找到。有关 gcloud CLI 的更多信息,请参阅其官方文档。
用法
您需要首先安装google-vertex-haystack 包才能使用VertexAIImageCaptioner:
pip install google-vertex-haystack
基本用法
from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator
generator = VertexAICodeGenerator()
result = generator.run(prefix="def to_json(data):")
for answer in result["replies"]:
print(answer)
>>> ```python
>>> import json
>>>
>>> def to_json(data):
>>> """Converts a Python object to a JSON string.
>>>
>>> Args:
>>> data: The Python object to convert.
>>>
>>> Returns:
>>> A JSON string representing the Python object.
>>> """
>>>
>>> return json.dumps(data)
>>> ```
您还可以设置其他参数,例如输出令牌数、温度、停止序列和候选数量。
让我们尝试一个不同的模型
from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator
generator = VertexAICodeGenerator(
model="code-gecko",
temperature=0.8,
candidate_count=3
)
result = generator.run(prefix="def convert_temperature(degrees):")
for answer in result["replies"]:
print(answer)
>>>
>>> return degrees * (9/5) + 32
>>>
>>> return round(degrees * (9.0 / 5.0) + 32, 1)
>>>
>>> return 5 * (degrees - 32) /9
>>>
>>> def convert_temperature_back(degrees):
>>> return 9 * (degrees / 5) + 32
更新于 大约 1 年前
