### Install and Run TypeScript Examples Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/en/AGENTS.md Use these commands to install dependencies, build, and start TypeScript examples. ```bash npm install npm run build npm start ``` -------------------------------- ### Node.js/TypeScript Project Setup Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/AGENTS.md Install root-level Node.js dependencies and then navigate to individual lesson directories to install project-specific dependencies. ```bash # Install root-level dependencies (for documentation tooling) npm install # For individual lesson TypeScript examples, navigate to the specific lesson: cd 06-text-generation-apps/typescript/recipe-app npm install ``` -------------------------------- ### Setup Virtual Environment and Install OpenAI Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/pcm/06-text-generation-apps/python/oai-assigment.ipynb Use these commands to create a virtual environment and install the OpenAI Python package. Note the Windows-specific activation command. ```bash # Create virtual environment ! python -m venv venv # Activate virtual environment ! source venv/bin/activate # Install openai package ! pip install openai ``` ```bash venv\Scripts\activate ``` -------------------------------- ### Python Virtual Environment Setup Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/AGENTS.md Set up a Python virtual environment and install project dependencies using pip. ```bash # Create virtual environment python3 -m venv venv # Activate virtual environment # On macOS/Linux: source venv/bin/activate # On Windows: virtualenv\Scripts\activate # Install dependencies pip install -r requirements.txt ``` -------------------------------- ### Install faiss-cpu Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/20-mistral/README.md Installs the faiss-cpu package, which is used as a vector store in the RAG example. ```python pip install faiss-cpu ``` -------------------------------- ### Create Python Web API with Flask Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/05-advanced-prompts/README.md Initial Flask application setup for API routes. Ensure Flask is installed. ```python import flask app = Flask(__name__) @app.route('/products') def products(): return 'Products' @app.route('/customers') def customers(): return 'Customers' ``` -------------------------------- ### Install ONNX Runtime Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/19-slm/README.md Install the ONNX Runtime library using pip. ```Python pip install onnxruntime ``` -------------------------------- ### Start JupyterHub Server Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/00-course-setup/README.md Navigate to your course directory in the terminal and run this command to start a local JupyterHub instance. ```bash jupyterhub ``` -------------------------------- ### Start Jupyter Notebook Server Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/00-course-setup/README.md Navigate to your course directory in the terminal and run this command to start a local Jupyter Notebook instance. ```bash jupyter notebook ``` -------------------------------- ### Install OpenCV Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/19-slm/python/phi35-vision-demo.ipynb Install the OpenCV library for video processing. This is a prerequisite for the keyframe extraction function. ```python # pip install opencv-python ``` -------------------------------- ### Install Flash Attention Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/19-slm/python/phi35_moe_demo.ipynb Installs Flash Attention, an optimized attention mechanism that can speed up training and inference. ```python # pip install flash-attn --no-build-isolation ``` ```python # ! pip install flash_attn -U ``` -------------------------------- ### Set up Virtual Environment and Install OpenAI Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/06-text-generation-apps/README.md Create a virtual environment and install the necessary OpenAI Python package. Use `venv\Scripts\activate` on Windows. ```bash python -m venv venv source venv/bin/activate pip install openai ``` -------------------------------- ### Generate Python Web API Code Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/05-advanced-prompts/README.md Basic Python Web API generation using Flask. This example includes a simple GET route and placeholder for data processing. Ensure Flask is installed. ```python # Import necessary modules import flask from flask import request, jsonify # Create a Flask app app = flask.Flask(__name__) # Create a route for the API @app.route('/api', methods=['GET']) def api(): # Get the data from the request data = request.args # Process the data result = process_data(data) # Return the result as JSON return jsonify(result) ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/00-course-setup/02-setup-local.md Use these commands to clone the course repository and navigate into the project directory. ```bash git clone https://github.com//generative-ai-for-beginners cd generative-ai-for-beginners ``` -------------------------------- ### Set up Python virtual environment and install libraries Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/09-building-image-applications/python/aoai-assignment.ipynb Create a virtual environment for your project and install the necessary Python libraries. This ensures dependency isolation. ```python # create virtual env ! python3 -m venv venv # activate environment ! source venv/bin/activate # install libraries # pip install -r requirements.txt, if using a requirements.txt file ! pip install python-dotenv openai pillow requests ``` -------------------------------- ### Clone Repository and Setup Environment Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/AGENTS.md Clone the repository, copy the environment template, and edit the .env file with your API keys and endpoints. ```bash # Clone the repository git clone https://github.com/microsoft/generative-ai-for-beginners.git cd generative-ai-for-beginners # Copy environment template cp .env.copy .env # Edit .env with your API keys and endpoints ``` -------------------------------- ### Few-Shot Prompting Example Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/05-advanced-prompts/README.md Provide examples within the prompt to guide the LLM's output format, style, or task understanding. This is useful when you need the LLM to adhere to a specific pattern or tone. ```text Write a poem in the style of Shakespeare. Here are a few examples of Shakespearean sonnets.: Sonnet 18: 'Shall I compare thee to a summer's day? Thou art more lovely and more temperate...' Sonnet 116: 'Let me not to the marriage of true minds Admit impediments. Love is not love Which alters when it alteration finds...' Sonnet 132: 'Thine eyes I love, and they, as pitying me, Knowing thy heart torment me with disdain,... Now, write a sonnet about the beauty of the moon. ``` -------------------------------- ### Build Documentation Site Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/en/AGENTS.md Execute this command to generate a PDF from the documentation if needed. ```bash # Generate PDF from documentation (if needed) npm run convert ``` -------------------------------- ### Validate OpenAI API Setup Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/04-prompt-engineering-fundamentals/python/oai-assignment.ipynb Verify your OpenAI API endpoint and key setup by sending a basic prompt and checking the completion. Ensure you have the 'openai' and 'python-dotenv' libraries installed and your API key configured. ```python # The OpenAI SDK was updated on Nov 8, 2023 with new guidance for migration # See: https://github.com/openai/openai-python/discussions/742 ## Updated import os from openai import OpenAI from dotenv import load_dotenv load_dotenv() client = OpenAI() deployment="gpt-3.5-turbo" ## Updated def get_completion(prompt): messages = [{"role": "user", "content": prompt}] response = client.chat.completions.create( model=deployment, messages=messages, temperature=0, # this is the degree of randomness of the model's output max_tokens=1024 ) return response.choices[0].message.content ## ---------- Call the helper method ### 1. Set primary content or prompt text text = f""" oh say can you see """ ### 2. Use that in the prompt template below prompt = f""" ```{text}``` """ ## 3. Run the prompt response = get_completion(prompt) print(response) ``` -------------------------------- ### Few-Shot Prompt Example with Shakespearean Style Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/zh-CN/05-advanced-prompts/README.md This prompt style guides the model by providing examples of the desired output format, context, or style. It's useful for tasks requiring specific creative or structural adherence. ```text Write a poem in the style of Shakespeare. Here are some examples of Shakespearean sonnets: Sonnet 18: "Shall I compare thee to a summer's day? Thou art more lovely and more temperate..." Sonnet 116: "Let me not to the marriage of true minds Admit impediments. Love is not love Which alters when it alteration finds..." Sonnet 132: "Thine eyes I love, and they in my love are. My love's dear eyes in praise do nothing else... Now write a sonnet about the beauty of the moon. ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/00-course-setup/README.md Clone the course repository to your local machine using git and then navigate into the cloned directory. This is the first step to running the code locally. ```shell git clone https://github.com/microsoft/generative-ai-for-beginners cd generative-ai-for-beginners ``` -------------------------------- ### Run Python Example Script Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/AGENTS.md Navigate to the lesson directory and execute a Python script using the python interpreter. ```bash # Navigate to lesson directory cd 06-text-generation-apps/python # Run a Python script python aoai-app.py ``` -------------------------------- ### Initialize OpenAI Client and Generate Recipes Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/06-text-generation-apps/python/oai-assigment.ipynb Loads environment variables, initializes the OpenAI client, and prompts the user for recipe details. It then constructs a prompt and uses the client to generate recipes. ```python from dotenv import load_dotenv import os from openai import OpenAI # load environment variables from .env file load_dotenv() client = OpenAI( api_key = os.environ['OPENAI_API_KEY'] ) deployment = "gpt-3.5-turbo" no_recipes = input("No of recipes (for example, 5: ") ingredients = input("List of ingredients (for example, chicken, potatoes, and carrots: ") # interpolate the number of recipes into the prompt an ingredients prompt = f"Show me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used" messages = [{"role": "user", "content": prompt}] # make completion completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600) # print response print(completion.choices[0].message.content) ``` -------------------------------- ### Generate Product Names using OpenAI Chat API Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/en/07-building-chat-applications/python/oai-assignment.ipynb This example demonstrates generating creative product names based on a description and seed words. The prompt includes examples to guide the model. 'client' and 'model' must be initialized. ```python prompt = "Product description: A home milkshake maker\nSeed words: fast, healthy, compact.\nProduct names: HomeShaker, Fit Shaker, QuickShake, Shake Maker\n\nProduct description: A pair of shoes that can fit any foot size.\nSeed words: adaptable, fit, omni-fit." ``` ```python #Setting a few additional, typical parameters during API Call response = client.chat.completions.create( model=model, messages = [{"role":"system", "content":"You are a helpful assistant."}, {"role":"user","content":prompt}]) response.choices[0].message.content ``` -------------------------------- ### Initialize OpenAI Client and Load Environment Variables Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/08-building-search-applications/python/oai-solution.ipynb Sets up the OpenAI client by loading the API key from a .env file. Asserts that the API key is present before proceeding. Defines model and constants for similarity threshold and dataset name. ```python import os import pandas as pd import numpy as np from openai import OpenAI from dotenv import load_dotenv load_dotenv() API_KEY = os.getenv("OPENAI_API_KEY","") assert API_KEY, "ERROR: OpenAI Key is missing" client = OpenAI( api_key=API_KEY ) model = 'text-embedding-ada-002' SIMILARITIES_RESULTS_THRESHOLD = 0.75 DATASET_NAME = "../embedding_index_3m.json" ``` -------------------------------- ### Instruction-Based Prompting for Summarization Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/pcm/04-prompt-engineering-fundamentals/python/oai-assignment.ipynb Use the 'text' variable for content and the 'prompt' variable for instructions to guide the model. This example demonstrates summarizing text for a specific audience (a second-grader). ```python # Test Example # https://platform.openai.com/playground/p/default-summarize ## Example text text = f""" Jupiter is the fifth planet from the Sun and the \ largest in the Solar System. It is a gas giant with \ a mass one-thousandth that of the Sun, but two-and-a-half \ times that of all the other planets in the Solar System combined. \ Jupiter is one of the brightest objects visible to the naked eye \ in the night sky, and has been known to ancient civilizations since \ before recorded history. It is named after the Roman god Jupiter.[19] \ When viewed from Earth, Jupiter can be bright enough for its reflected \ light to cast visible shadows,[20] and is on average the third-brightest \ natural object in the night sky after the Moon and Venus. """ ``` -------------------------------- ### Initialize OpenAI Client Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/08-building-search-applications/python/oai-assignment.ipynb Sets up the OpenAI client by loading the API key from environment variables. Ensure your OPENAI_API_KEY is set in your .env file. ```python import os from openai import OpenAI from dotenv import load_dotenv import numpy as np load_dotenv() API_KEY = os.getenv("OPENAI_API_KEY","") assert API_KEY, "ERROR: OpenAI Key is missing" client = OpenAI( api_key=API_KEY ) ``` -------------------------------- ### Study Buddy Prompt Example Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/06-text-generation-apps/README.md A starter prompt for a 'study buddy' application focused on the Python language. This prompt guides the AI to provide lessons, explanations, and code exercises. ```text - "You're an expert on the Python language Suggest a beginner lesson for Python in the following format: Format: - concepts: - brief explanation of the lesson: - exercise in code with solutions" ``` -------------------------------- ### Initialize OpenAI Client Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/07-building-chat-applications/python/oai-assigment-simple.ipynb Sets up the OpenAI client using an API key loaded from environment variables. Ensure the OPENAI_API_KEY is set. ```python import os from openai import OpenAI from dotenv import load_dotenv load_dotenv() API_KEY = os.getenv("OPENAI_API_KEY","").strip() assert API_KEY, "ERROR: OpenAI Key is missing" client = OpenAI( api_key=API_KEY ) model = "gpt-3.5-turbo" ``` -------------------------------- ### Instruction-Based LLM Tasks Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/04-prompt-engineering-fundamentals/python/aoai-assignment.ipynb Utilize the 'text' variable for primary content and the 'prompt' variable for instructions to guide the LLM. This example demonstrates summarizing text for a specific audience (second-grade student). ```python # Test Example # https://platform.openai.com/playground/p/default-summarize ``` -------------------------------- ### Generate Product Names Prompt Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/07-building-chat-applications/python/aoai-assignment.ipynb Constructs a prompt for generating product names by providing a product description and seed words, along with examples. Use this to guide the model's creative output. ```python prompt = "Product description: A home milkshake maker\nSeed words: fast, healthy, compact.\nProduct names: HomeShaker, Fit Shaker, QuickShake, Shake Maker\n\nProduct description: A pair of shoes that can fit any foot size.\nSeed words: adaptable, fit, omni-fit." print(prompt) ``` -------------------------------- ### Chain-of-Thought Prompting for Mathematical Problems Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/zh-CN/05-advanced-prompts/README.md This technique guides the LLM by showing it how to break down a problem into steps, similar to providing a worked example. Use this for complex reasoning or calculation tasks where intermediate steps are crucial for accuracy. ```text Lisa has 7 apples, threw away 1 apple, gave 4 apples to Bart, and Bart gave one back: 7 - 1 = 6 6 - 4 = 2 2 + 1 = 3 Alice has 5 apples, threw away 3 apples, gave 2 apples to Bob, and Bob gave one back: Alice has how many apples? ``` -------------------------------- ### Initialize OpenAI Client and Load Environment Variables Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/11-integrating-with-function-calling/python/oai-assignment.ipynb Import necessary libraries (os, json, OpenAI, dotenv) and load environment variables for API key management. Initialize the OpenAI client and set the deployment model. ```python import os import json from openai import OpenAI from dotenv import load_dotenv load_dotenv() client = OpenAI() deployment="gpt-3.5-turbo" ``` -------------------------------- ### System Message for Agent Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/translations/fr/19-slm/python/phi35_moe_demo.ipynb Defines a system message that guides an AI agent, outlining available tools and the required JSON format for tool usage. This message includes examples for using 'Blog' and 'Translate' tools. ```python sys_msg = """You are a helpful AI assistant, you are an agent capable of using a variety of tools to answer a question. Here are a few of the tools available to you: - Blog: This tool helps you describe a certain knowledge point and content, and finally write it into Twitter or Facebook style content - Translate: This is a tool that helps you translate into any language, using plain language as required To use these tools you must always respond in JSON format containing \"tool_name\" and \"input\" key-value pairs. For example, to answer the question, \"Build Muliti Agents with MOE models\" you must use the calculator tool like so: ```json { \"tool_name\": \"Blog\", \"input\": \"Build Muliti Agents with MOE models\" } ``` Or to translate the question \"can you introduce yourself in Chinese\" you must respond: ```json { \"tool_name\": \"Search\", \"input\": \"can you introduce yourself in Chinese\" } ``` Remember just output the final result, output in JSON format containing \"agentid\",\"tool_name\" , \"input\" and \"output\" key-value pairs .: ```json [ { \"agentid\": \"step1\", \"tool_name\": \"Blog\", \"input\": \"Build Muliti Agents with MOE models\", \"output\": \".........\" }, { \"agentid\": \"step2\", \"tool_name\": \"Search\", \"input\": \"can you introduce yourself in Chinese\", \"output\": \".........\" }, { \"agentid\": \"final\" \"tool_name\": \"Result\", \"output\": \".........\" } ] ``` The users answer is as follows. """ } ] }, { ``` ```python def instruction_format(sys_message: str, query: str): # note, don't "" to the end return f'<|system|> {sys_message} <|end|> <|user|> {query} <|end|> <|assistant|>' ``` ```python query ='Write something about Generative AI with MOE , translate it to Chinese' ``` ```python input_prompt = instruction_format(sys_msg, query) ``` ```python input_prompt ``` -------------------------------- ### Initialize Azure OpenAI Client and Load Environment Variables Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/08-building-search-applications/python/aoai-solution.ipynb Sets up the Azure OpenAI client using API key and deployment details from environment variables. Ensure your .env file contains AZURE_OPENAI_API_KEY and AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT. ```python import os import pandas as pd import numpy as np from openai import AzureOpenAI from dotenv import load_dotenv load_dotenv() client = AzureOpenAI( api_key=os.environ['AZURE_OPENAI_API_KEY'], # this is also the default, it can be omitted api_version = "2023-05-15" ) model = os.environ['AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT'] SIMILARITIES_RESULTS_THRESHOLD = 0.75 DATASET_NAME = "../embedding_index_3m.json" ``` -------------------------------- ### Build and Run TypeScript Application Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/AGENTS.md Navigate to the TypeScript application directory, build the project, and then start the application. ```bash # Navigate to TypeScript app directory cd 06-text-generation-apps/typescript/recipe-app # Build the TypeScript code npm run build # Run the application npm start ``` -------------------------------- ### Install OpenAI Package Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/06-text-generation-apps/python/aoai-assignment.ipynb Use these commands to create a virtual environment and install the OpenAI Python package. Ensure you activate the environment before installation. Note the Windows-specific activation command. ```python # Create virtual environment ! python -m venv venv # Activate virtual environment ! source venv/bin/activate # Install openai package ! pip install openai ``` -------------------------------- ### Set and Run a Simple Prompt Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/04-prompt-engineering-fundamentals/python/oai-assignment.ipynb Construct a prompt for summarization and execute it using the get_completion function. Ensure the text to be summarized is properly formatted within the prompt. ```python prompt = f" Summarize content you are provided with for a second-grade student. ```{text}``` " ## Run the prompt response = get_completion(prompt) print(response) ``` -------------------------------- ### Verify Conda Installation Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/00-course-setup/02-setup-local.md Check if Conda is installed and accessible from the command line. ```bash conda --version ``` -------------------------------- ### Initialize Azure OpenAI Client Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/11-integrating-with-function-calling/python/aoai-assignment.ipynb Set up the Azure OpenAI client using API key, endpoint, and deployment name. Ensure environment variables are loaded. ```python import os import json from openai import AzureOpenAI from dotenv import load_dotenv load_dotenv() client = AzureOpenAI( api_key=os.environ['AZURE_OPENAI_API_KEY'], # this is also the default, it can be omitted api_version = "2023-07-01-preview" ) deployment=os.environ['AZURE_OPENAI_DEPLOYMENT'] ``` -------------------------------- ### Verify Docker Installation Source: https://github.com/microsoft/generative-ai-for-beginners/blob/main/00-course-setup/02-setup-local.md Check if Docker is installed and accessible from the command line. ```bash docker --version ```