Last active
November 7, 2024 11:08
-
-
Save unixzen/b41e87c0f22b1f4cacce482ce617a7e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import httpx | |
| import os | |
| import json | |
| BASE_YC_GPT_URL = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion" | |
| calculator_tool = { | |
| "function": { | |
| "name": "calculator", | |
| "description": "Performs basic arithmetic operations", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "operation": { | |
| "type": "string", | |
| "enum": ["add", "subtract", "multiply", "divide"], | |
| "description": "The arithmetic operation to perform" | |
| }, | |
| "number1": { | |
| "type": "number", | |
| "description": "First number" | |
| }, | |
| "number2": { | |
| "type": "number", | |
| "description": "Second number" | |
| } | |
| }, | |
| "required": ["operation", "number1", "number2"] | |
| } | |
| } | |
| } | |
| def make_request_yc_gpt(text): | |
| with httpx.Client() as client: | |
| headers = {'Authorization': "Api-Key " + os.environ['YC_API_KEY'], | |
| 'content-type':'application/json'} | |
| payload = { | |
| "modelUri": f"gpt://{os.environ['YC_FOLDER_ID']}/yandexgpt-32k/rc", | |
| "completionOptions": { | |
| "stream": False, | |
| "temperature": 0.3, | |
| "maxTokens": 8000 | |
| }, | |
| "messages": text, | |
| "tools": [calculator_tool] # Add the tools definition here | |
| } | |
| r = client.post( | |
| BASE_YC_GPT_URL, | |
| timeout=None, | |
| json=payload, | |
| headers=headers | |
| ) | |
| response = r.json() | |
| return response#["result"]["alternatives"][0]["message"]["text"] | |
| def handle_tool_calls(toolCalls): | |
| print(toolCalls) | |
| results = [] | |
| for tool_call in toolCalls: | |
| if toolCalls[0]["functionCall"]["name"] == "calculator": | |
| print(type(tool_call)) | |
| print(tool_call) | |
| #args = json.loads(tool_call["functionCall"]["arguments"]) | |
| #print(type(args)) | |
| #print(args) | |
| result = perform_calculation(tool_call["functionCall"]["arguments"]) | |
| results.append({ | |
| "output": str(result) | |
| }) | |
| return results | |
| def perform_calculation(args): | |
| num1 = args["number1"] | |
| num2 = args["number2"] | |
| operation = args["operation"] | |
| if operation == "add": | |
| return num1 + num2 | |
| elif operation == "subtract": | |
| return num1 - num2 | |
| elif operation == "multiply": | |
| return num1 * num2 | |
| elif operation == "divide": | |
| return num1 / num2 if num2 != 0 else "Error: Division by zero" | |
| def process_conversation(user_input): | |
| conversation = [] | |
| # Add user message | |
| conversation.append({ | |
| "role": "user", | |
| "text": user_input | |
| }) | |
| response = make_request_yc_gpt(conversation) | |
| print(response) | |
| #print("============================================================") | |
| if not response: | |
| return "Error processing request" | |
| #return response | |
| # | |
| if "toolCalls" in response['result']['alternatives'][0]['message']['toolCallList']: | |
| print("True") | |
| tool_results = handle_tool_calls(response['result']['alternatives'][0]['message']['toolCallList']["toolCalls"]) | |
| print(tool_results) | |
| for tool_result in tool_results: | |
| conversation.append({ | |
| "role": "assistant", | |
| "text": tool_result["output"] | |
| }) | |
| final_response = conversation[-1]["text"] | |
| return final_response | |
| else: | |
| print("False") | |
| return response | |
| def main(): | |
| # Example usage | |
| user_input = "Can you help me calculate 25 plus 17?" | |
| result = process_conversation(user_input) | |
| print(f"User: {user_input}") | |
| print(f"Assistant: {result}") | |
| """ | |
| # Another example | |
| user_input = "What is 100 divided by 5?" | |
| result = process_conversation(user_input) | |
| print(f"\nUser: {user_input}") | |
| print(f"Assistant: {result}") | |
| user_input = "В чем смысл жизни?" | |
| result = process_conversation(user_input) | |
| print(f"\nUser: {user_input}") | |
| print(f"Assistant: {result}")""" | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment