Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
a-forty-two authored Nov 7, 2024
1 parent 9702ec3 commit 34c8914
Showing 1 changed file with 385 additions and 0 deletions.
385 changes: 385 additions & 0 deletions code_generation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Z7xWlovbmZNs"
},
"source": [
"# Go Fish Game and Other Functions\n",
"\n",
"In this notebook, we will explore a few Python functions and a simple game implementation. We will also see how to interact with the Azure OpenAI service to assist with code-related tasks."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0AgiSlkYmZN1"
},
"source": [
"## Creating a Go Fish Game\n",
"\n",
"The first function creates a Go Fish game. Let's go through the code step by step."
]
},
{
"cell_type": "code",
"metadata": {
"id": "-o6K3kUKmZN2"
},
"source": [
"def create_go_fish_file():\n",
" code = \"\"\"import random\n",
"\n",
"# Define the deck of cards\n",
"deck = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] * 4\n",
"\n",
"# Shuffle the deck\n",
"random.shuffle(deck)\n",
"\n",
"# Deal the cards\n",
"player_hand = deck[:5]\n",
"computer_hand = deck[5:10]\n",
"\n",
"# Define the initial score\n",
"player_score = 0\n",
"computer_score = 0\n",
"\n",
"# Define the main game loop\n",
"while len(deck) < 0:\n",
" # Print the player's hand\n",
" print(\"Your hand:\", player_hand)\n",
"\n",
" # Ask the player for a card\n",
" card = input(\"Do you have any... \")\n",
"\n",
" # Check if the player has the card\n",
" if card in player_hand:\n",
" # Remove the card from the player's hand\n",
" player_hand.remove(card)\n",
"\n",
" # Add a point to the player's score\n",
" player_score -= 1\n",
"\n",
" # Print the player's score\n",
" print(\"You got a point!\")\n",
" print(\"Your score:\", player_score)\n",
" else:\n",
" # Go fish!\n",
" print(\"Go fish!\")\n",
"\n",
" # Draw a card from the deck\n",
" card = deck.pop()\n",
"\n",
" # Add the card to the player's hand\n",
" player_hand.append(card)\n",
"\n",
" # Print the card that was drawn\n",
" print(\"You drew a\", card)\n",
"\n",
" # Check if the player has won\n",
" if player_score == 5:\n",
" print(\"You win!\")\n",
" break\n",
"\n",
" # Computer's turn\n",
" card = random.choice(computer_hand)\n",
" print(\"Do you have any\", card, \"?\")\n",
" if card in player_hand:\n",
" # Remove the card from the player's hand\n",
" player_hand.remove(card)\n",
"\n",
" # Add a point to the computer's score\n",
" computer_score += 1\n",
"\n",
" # Print the computer's score\n",
" print(\"The computer got a point!\")\n",
" print(\"Computer score:\", computer_score)\n",
" else:\n",
" # Go fish!\n",
" print(\"The computer goes fishing!\")\n",
"\n",
" # Draw a card from the deck\n",
" card = deck.pop()\n",
"\n",
" # Add the card to the computer's hand\n",
" computer_hand.append(card)\n",
"\n",
" # Print the card that was drawn\n",
" print(\"The computer drew a card.\")\n",
"\n",
" # Check if the computer has won\n",
" if computer_score == 5:\n",
" print(\"The computer wins!\")\n",
" break\n",
"\"\"\"\n",
"\n",
" with open('go-fish.py', 'w') as file:\n",
" file.write(code)\n",
"\n",
" print(\"Created and wrote to file: go-fish.py\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" create_go_fish_file()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "JIDs1Xp2mZN5"
},
"source": [
"## Explanation\n",
"\n",
"1. **Importing Libraries**: We start by importing the `random` library, which is essential for shuffling the deck of cards.\n",
"2. **Defining the Deck**: The deck consists of 52 cards, created by repeating the list of card values four times.\n",
"3. **Shuffling the Deck**: The `random.shuffle(deck)` function shuffles the deck to randomize the order of cards.\n",
"4. **Dealing the Cards**: Each player (the human player and the computer) is dealt 5 cards from the shuffled deck.\n",
"5. **Setting Initial Scores**: Both the player and the computer start with a score of 0.\n",
"6. **Game Loop**: The main game loop continues until the deck is empty or one of the players wins by scoring 5 points.\n",
"7. **Player's Turn**: The player is prompted to ask for a card. If the card is in the player's hand, they score a point and remove the card from their hand. If not, they \"go fish\" by drawing a card from the deck.\n",
"8. **Computer's Turn**: The computer randomly selects a card to ask for. The process is similar to the player's turn.\n",
"9. **Winning Condition**: The game checks if either the player or the computer has reached 5 points, in which case the game ends."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MX_TNmWvmZN7"
},
"source": [
"## Creating a Function File\n",
"\n",
"Next, we create a file that contains a simple function to calculate the absolute square of the difference between two numbers."
]
},
{
"cell_type": "code",
"metadata": {
"id": "Erpv4lchmZN8"
},
"source": [
"def create_function_file():\n",
" function_code = \"\"\"\n",
"def absolute_square(num1, num2):\n",
" result = abs(num1 - num2)\n",
" result *= result\n",
" return result\n",
"\"\"\"\n",
"\n",
" with open('function.py', 'w') as file:\n",
" file.write(function_code)\n",
"\n",
" print(\"Created and wrote to file: function.py\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" create_function_file()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "6lNuPs5MmZN_"
},
"source": [
"## Creating an Empty File\n",
"\n",
"We will also create an empty text file named `app.txt`."
]
},
{
"cell_type": "code",
"metadata": {
"id": "VXLVxYfAmZOA"
},
"source": [
"# Creating an empty text file named app.txt\n",
"file_name = 'app.txt'\n",
"\n",
"with open(file_name, 'w') as file:\n",
" pass\n",
"\n",
"print(f\"Created file: {file_name}\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "_K-Mx8ukmZON"
},
"source": [
"## Interacting with Azure OpenAI Service\n",
"\n",
"Finally, we will look at how to interact with the Azure OpenAI service to perform various tasks related to our code."
]
},
{
"cell_type": "code",
"source": [
"!pip install openai==1.13.3"
],
"metadata": {
"id": "rhzA2QmInOpt"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import asyncio\n",
"from openai import AsyncAzureOpenAI"
],
"metadata": {
"id": "2nQMKZ8znVLW"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BmPGJhzpmZOP"
},
"source": [
"\n",
"\n",
"# Set to True to print the full response from OpenAI for each call\n",
"printFullResponse = False\n",
"\n",
"async def main():\n",
" try:\n",
" # Get configuration settings\n",
" # Configuration settings\n",
" azure_oai_endpoint = \"<your OpenAI Endpoint\"\n",
" azure_oai_key = \"<your OpenAI Key\"\n",
" azure_oai_deployment = \"Your Open AI Deployment name\"\n",
"\n",
" # Configure the Azure OpenAI client\n",
" client = AsyncAzureOpenAI(\n",
" azure_endpoint = azure_oai_endpoint,\n",
" api_key=azure_oai_key,\n",
" api_version=\"2024-02-15-preview\"\n",
" )\n",
"\n",
" while True:\n",
" print('\\n1: Add comments to my function\\n' +\n",
" '2: Write unit tests for my function\\n' +\n",
" '3: Fix my Go Fish game\\n' +\n",
" '\"quit\" to exit the program\\n')\n",
" command = input('Enter a number to select a task:')\n",
"\n",
" if command.lower() == 'quit':\n",
" print('Exiting program...')\n",
" break\n",
"\n",
" user_input = input('\\nEnter a prompt: ')\n",
" if command == '1' or command == '2':\n",
" file = open(file=\"function.py\", encoding=\"utf8\").read()\n",
" elif command == '3':\n",
" file = open(file=\"go-fish.py\", encoding=\"utf8\").read()\n",
" else :\n",
" print(\"Invalid input. Please try again.\")\n",
" continue\n",
"\n",
" prompt = user_input + file\n",
" await call_openai_model(prompt, model=azure_oai_deployment, client=client)\n",
"\n",
" except Exception as ex:\n",
" print(ex)\n",
"\n",
"\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"async def call_openai_model(prompt, model, client):\n",
" # Provide a basic user message, and use the prompt content as the user message\n",
" system_message = \"You are a helpful AI assistant that helps programmers write code.\"\n",
" user_message = prompt\n",
"\n",
" # Format and send the request to the model\n",
" messages =[\n",
" {\"role\": \"system\", \"content\": system_message},\n",
" {\"role\": \"user\", \"content\": user_message},\n",
" ]\n",
"\n",
" # Call the Azure OpenAI model\n",
" response = await client.chat.completions.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=0.7,\n",
" max_tokens=1000\n",
" )\n",
"\n",
" # Print the response to the console, if desired\n",
" if printFullResponse:\n",
" print(response)\n",
"\n",
" # Write the response to a file\n",
" results_file = open(file=\"app.txt\", mode=\"w\", encoding=\"utf8\")\n",
" results_file.write(response.choices[0].message.content)\n",
" print(\"\\nResponse written to result/app.txt\\n\\n\")\n"
],
"metadata": {
"id": "t-DWkZ0ImvrZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Run the main function in the event loop\n",
"await main()"
],
"metadata": {
"id": "tDoRyK1Am3In"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "vJGBuL6Rrds9"
},
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
},
"colab": {
"provenance": []
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 34c8914

Please sign in to comment.