Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemini request mech tool #228

Merged
merged 9 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .example.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FILE_HASH_TO_TOOLS='[["bafybeibdcttrlgp5udygntka5fofi566pitkxhquke37ng7csvndhy4s2i",["openai-gpt-3.5-turbo-instruct","openai-gpt-3.5-turbo","openai-gpt-4"]],["bafybeiegbsq2ajxyipajac4mmxyvbt22ctwyuypuid6ziavqpndns6fsjy",["stabilityai-stable-diffusion-v1-5","stabilityai-stable-diffusion-xl-beta-v2-2-2","stabilityai-stable-diffusion-512-v2-1","stabilityai-stable-diffusion-768-v2-1"]],["bafybeihugaylajwh2fgypxatcw5qrw5qxadtrsi2h2s2246442wlvjirtm",["transfer-native"]],["bafybeiayity7o6c2yynrpe6libyak37hgf4fp7a4kjfcnqkkxwy3zkp3ie",["prediction-offline","prediction-online"]]]'
API_KEYS='[["openai","dummy_api_key"],["stabilityai","dummy_api_key"]]'
FILE_HASH_TO_TOOLS='[["bafybeibdcttrlgp5udygntka5fofi566pitkxhquke37ng7csvndhy4s2i",["openai-gpt-3.5-turbo-instruct","openai-gpt-3.5-turbo","openai-gpt-4"]],["bafybeiegbsq2ajxyipajac4mmxyvbt22ctwyuypuid6ziavqpndns6fsjy",["stabilityai-stable-diffusion-v1-5","stabilityai-stable-diffusion-xl-beta-v2-2-2","stabilityai-stable-diffusion-512-v2-1","stabilityai-stable-diffusion-768-v2-1"]],["bafybeihugaylajwh2fgypxatcw5qrw5qxadtrsi2h2s2246442wlvjirtm",["transfer-native"]],["bafybeiayity7o6c2yynrpe6libyak37hgf4fp7a4kjfcnqkkxwy3zkp3ie",["prediction-offline","prediction-online"]],["bafybeifpalgiya4dyu42hehsqxlwr36fjtxdo75xf3gcpfrauvoxgargy4",["gemini-pro", "gemini-1.0-pro-001", "gemini-1.0-pro-latest", "gemini-1.5-pro-latest"]]]'
API_KEYS='[["openai","dummy_api_key"],["stabilityai","dummy_api_key"],["gemini","dummy_api_key"]]'
ETHEREUM_LEDGER_RPC_0=https://rpc.eu-central-2.gateway.fm/v4/gnosis/non-archival/mainnet
GNOSIS_RPC_0=https://rpc.eu-central-2.gateway.fm/v4/gnosis/non-archival/mainnet
ETHEREUM_WEBSOCKET_RPC_0=wss://rpc.eu-central-2.gateway.fm/ws/v4/gnosis/non-archival/mainnet
Expand Down
1 change: 1 addition & 0 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"custom/napthaai/prediction_request_reasoning_claude/0.1.0": "bafybeihtx2cejxoy42jwk2i5m4evfzz537aic5njuawxnzdzwlo63kdduq",
"custom/napthaai/prediction_request_rag_claude/0.1.0": "bafybeickr32t7nmapuoymjyo3cf5rr2v2zapksxcivuqsgjr2gn6zo6y7y",
"custom/napthaai/prediction_request_rag_cohere/0.1.0": "bafybeie2qi27usujclcje524qu4w6iv3viouq3pxhs2yft3aw26nnerla4",
"custom/victorpolisetty/gemini_request/0.1.0": "bafybeifpalgiya4dyu42hehsqxlwr36fjtxdo75xf3gcpfrauvoxgargy4",
"protocol/valory/acn_data_share/0.1.0": "bafybeih5ydonnvrwvy2ygfqgfabkr47s4yw3uqxztmwyfprulwfsoe7ipq",
"protocol/valory/websocket_client/0.1.0": "bafybeifjk254sy65rna2k32kynzenutujwqndap2r222afvr3zezi27mx4",
"contract/valory/agent_mech/0.1.0": "bafybeiah6b5epo2hlvzg5rr2cydgpp2waausoyrpnoarf7oa7bw33rex34",
Expand Down
19 changes: 19 additions & 0 deletions packages/victorpolisetty/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------
19 changes: 19 additions & 0 deletions packages/victorpolisetty/customs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------
20 changes: 20 additions & 0 deletions packages/victorpolisetty/customs/gemini_request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the bet amount per threshold strategy."""
17 changes: 17 additions & 0 deletions packages/victorpolisetty/customs/gemini_request/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: gemini_request
author: victorpolisetty
version: 0.1.0
type: custom
description: A tool that runs a prompt against Google Gemini.
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeibbn67pnrrm4qm3n3kbelvbs3v7fjlrjniywmw2vbizarippidtvi
gemini_request.py: bafybeigae5an6ewsboran33o2x7d6itcoxwgrnoucoluhsyrzp462afida
fingerprint_ignore_patterns: []
entry_point: gemini_request.py
callable: run
dependencies:
google-generativeai:
version: ==0.5.3
requests: {}
78 changes: 78 additions & 0 deletions packages/victorpolisetty/customs/gemini_request/gemini_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------
"""Contains the job definitions"""

from typing import Any, Dict, Optional, Tuple

import google.generativeai as genai

DEFAULT_GEMINI_SETTINGS = {
"candidate_count": 1,
"stop_sequences": None,
"max_output_tokens": 500,
"temperature": 0.7,
}
PREFIX = "gemini-"
ENGINES = {
"chat": ["pro", "1.0-pro-001", "1.0-pro-latest", "1.5-pro-latest"],
}

ALLOWED_TOOLS = [PREFIX + value for value in ENGINES["chat"]]

def count_tokens(text: str) -> int:
"""Count the number of tokens in a text using the Gemini model's tokenizer."""
return genai.count_message_tokens(prompt=text)

def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any, Any]:
"""Run the task"""

api_key = kwargs["api_keys"]["gemini"]
tool = kwargs["tool"]
prompt = kwargs["prompt"]

if tool not in ALLOWED_TOOLS:
return f"Model {tool} is not in the list of supported models.", None, None, None,

candidate_count = kwargs.get("candidate_count", DEFAULT_GEMINI_SETTINGS["candidate_count"])
stop_sequences = kwargs.get("stop_sequences", DEFAULT_GEMINI_SETTINGS["stop_sequences"])
max_output_tokens = kwargs.get("max_output_tokens", DEFAULT_GEMINI_SETTINGS["max_output_tokens"])
temperature = kwargs.get("temperature", DEFAULT_GEMINI_SETTINGS["temperature"])

counter_callback = kwargs.get("counter_callback", None)

genai.configure(api_key=api_key)
engine = genai.GenerativeModel(tool)

try:
response = engine.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
candidate_count=candidate_count,
stop_sequences=stop_sequences,
max_output_tokens=max_output_tokens,
temperature=temperature)
)

# Ensure response has a .text attribute
response_text = getattr(response, 'text', None)

except Exception as e:
return f"An error occurred: {str(e)}", None, None, None

return response.text, prompt, None, counter_callback
victorpolisetty marked this conversation as resolved.
Show resolved Hide resolved
Loading