Created
October 29, 2024 10:38
-
-
Save 7effrey89/fa8cc91b8a70a7cfa4b25526ce195ea7 to your computer and use it in GitHub Desktop.
GPT4o/mini translation sample that translate an input and calculates the costs based on Sweden Central
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
| import os | |
| import requests | |
| import json | |
| ################################## Facts ###################################################### | |
| #GPT costs for GPT-4o and GPT-4o-Mini for Sweden Central per 1000 tokens | |
| #https://azure.microsoft.com/en-us/pricing/calculator | |
| GPT4O_GLOBAL_DEPLOYMENT_INPUT = 0.0025 | |
| GPT4O_GLOBAL_DEPLOYMENT_OUTPUT = 0.0100 | |
| GPT4O_MINI_GLOBAL_DEPLOYMENT_INPUT = 0.0002 | |
| GPT4O_MINI_GLOBAL_DEPLOYMENT_OUTPUT = 0.0006 | |
| ################################## Functions ###################################################### | |
| def Call_GPT(Model: str, System_Message: str, Message: str): | |
| """ | |
| Call the GPT model with the specified parameters. | |
| Args: | |
| model (str): The model type, e.g., "gpt-4o" or "gpt-4o-mini". | |
| system_message (str): The system message to set the context for the model. | |
| user_message (str): The user message to be processed by the model. | |
| Returns: | |
| dict: The JSON response from the GPT model. | |
| """ | |
| ENDPOINT = f"https://<Your-Resource>.openai.azure.com/openai/deployments/{Model.lower()}/chat/completions?api-version=2024-02-15-preview" | |
| API_KEY = "<Your-API-Key>" | |
| headers = { | |
| "Content-Type": "application/json", | |
| "api-key": API_KEY, | |
| } | |
| # Payload for the request | |
| payload = { | |
| "messages": [ | |
| { | |
| "role": "system", | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": f"{System_Message}" | |
| } | |
| ] | |
| }, | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": f"{Message}" | |
| } | |
| ] | |
| } | |
| ], | |
| "temperature": 0.0, | |
| "top_p": 0.95, | |
| "max_tokens": 800 | |
| } | |
| # Send request | |
| try: | |
| response = requests.post(ENDPOINT, headers=headers, json=payload) | |
| response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code | |
| except requests.RequestException as e: | |
| raise SystemExit(f"Failed to make the request. Error: {e}") | |
| # Handle the response as needed (e.g., print or process) | |
| response_json = response.json() | |
| message_content = response_json['choices'][0]['message']['content'] | |
| prompt_tokens = response_json['usage']['prompt_tokens'] | |
| completion_tokens = response_json['usage']['completion_tokens'] | |
| total_tokens = response_json['usage']['total_tokens'] | |
| return response_json | |
| def priceCalculator(response_json, model): | |
| """ | |
| Calculate the cost based on the response JSON and model type. | |
| Args: | |
| response_json (dict): The JSON response from the GPT model. | |
| model (str): The model type, e.g., "GPT-4o" or "GPT-4o-Mini". | |
| Returns: | |
| dict: A dictionary containing prompt tokens, completion tokens, and total cost. | |
| """ | |
| prompt_tokens = response_json['usage']['prompt_tokens'] | |
| completion_tokens = response_json['usage']['completion_tokens'] | |
| inputprice = 0 | |
| outputprice = 0 | |
| if model.lower() == "gpt-4o": | |
| #USD per 1000 tokens Sweden | |
| inputprice = GPT4O_GLOBAL_DEPLOYMENT_INPUT | |
| outputprice = GPT4O_GLOBAL_DEPLOYMENT_OUTPUT | |
| if model.lower() == "gpt-4o-mini": | |
| inputprice = GPT4O_MINI_GLOBAL_DEPLOYMENT_INPUT | |
| outputprice = GPT4O_MINI_GLOBAL_DEPLOYMENT_OUTPUT | |
| prompt_tokens_cost = prompt_tokens /1000 * inputprice | |
| completion_tokens_cost = completion_tokens /1000 * outputprice | |
| total_cost = prompt_tokens_cost + completion_tokens_cost | |
| return {"prompt_tokens": prompt_tokens, "completion_tokens": completion_tokens, "total_cost": total_cost} | |
| def OutputFormatter(response_json, response_cost): | |
| """ | |
| Format and print the output based on the response JSON and calculated cost. | |
| Args: | |
| response_json (dict): The JSON response from the GPT model. | |
| response_cost (dict): The calculated cost including prompt tokens, completion tokens, and total cost. | |
| """ | |
| message_content = response_json['choices'][0]['message']['content'] | |
| prompt_tokens = response_cost['prompt_tokens'] | |
| completion_tokens = response_cost['completion_tokens'] | |
| total_cost = response_cost['total_cost'] | |
| model = response_json['model'] | |
| print (model ,": ", message_content, "\n\nPrompt Tokens:", prompt_tokens," | Completion Tokens:", completion_tokens, " | Total Cost:", total_cost, "USD") | |
| ################################## Configuration ###################################################### | |
| #Choose the model to use | |
| model="gpt-4o" #"GPT-4o-mini" | |
| #Get the translatetion from the model | |
| response_json = Call_GPT(model, | |
| "Always translate received input into formal English.", | |
| """Der er ingen tvivl om, at det er følsomme data, der indgår i forskningsprojektet iPsych på Aarhus Universitet. Det understregede forskerne selv, da de for tolv år siden søgte myndighederne om at få projektet godkendt. | |
| De forudså blandt andet, at forskningen i gener kunne ændre vores forestillinger om sundhed og sygdom og den måde, vi opfatter os selv og hinanden på. Og de vurderede, at forskningen ville få store følgevirkninger på samfundet. | |
| Derfor lovede de, at de ville etablere en etisk rådgivningsgruppe, der skulle skabe den rigtige etiske ramme om projektet. | |
| Projektet blev godkendt, og siden har 140.000 danskere i alderen 16 til 43 år uden at vide det fået kortlagt deres gener i projektet. Gendataene er koblet sammen med oplysninger om psykiatriske diagnoser og en lang række andre data. | |
| """ | |
| ) | |
| #Calculate the cost of the translation | |
| response_cost = priceCalculator(response_json, model) # response_cost = priceCalculator(response_json, "GPT-4o-Mini") | |
| #Print the message and the cost | |
| OutputFormatter(response_json, response_cost) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment