{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "from azure.ai.inference import ChatCompletionsClient\n", "from azure.ai.inference.models import SystemMessage, UserMessage\n", "from azure.core.credentials import AzureKeyCredential" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "endpoint = \"https://models.inference.ai.azure.com\"\n", "model_name = \"Phi-3.5-mini-instruct\"\n", "token = \"Your GitHub Models Token\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "client = ChatCompletionsClient(\n", " endpoint=endpoint,\n", " credential=AzureKeyCredential(token),\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "response = client.complete(\n", " messages=[\n", " SystemMessage(content=\"You are my Python programming assistant,helping me generate Python code\"),\n", " UserMessage(content=\"create a flask restful api project\"),\n", " ],\n", " model=model_name,\n", " temperature=1.0,\n", " max_tokens=3096,\n", " top_p=1.0\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' To create a Flask RESTful API project, follow the steps below:\\n\\n1. **Set up development environment**\\n\\nYou first need to have Python installed on your machine. The latest version of Python can be downloaded from the official website: https://www.python.org/downloads/\\n\\n2. **Install Flask**\\n\\nFlask is a popular lightweight web framework for Python. You can install it using pip, the Python package installer. Run the following command in your terminal:\\n\\n```\\npip install Flask\\n```\\n\\n3. **Install Flask-RESTful**\\n\\nFlask-RESTful is an extension for Flask that adds support for quickly building REST APIs. Install it using pip:\\n\\n```\\npip install flask-restful\\n```\\n\\n4. **Create a new Python file**\\n\\nLet\\'s name the file `app.py`. Open this file and write the following code:\\n\\n```python\\nfrom flask import Flask\\nfrom flask_restful import Resource, Api\\n\\napp = Flask(__name__)\\napi = Api(app)\\n\\nclass HelloWorld(Resource):\\n def get(self):\\n return {\\'hello\\': \\'world\\'}\\n\\napi.add_resource(HelloWorld, \\'/\\')\\n\\nif __name__ == \\'__main__\\':\\n app.run(debug=True)\\n```\\n\\nThis code sets up a basic Flask application with one basic resource, the `HelloWorld` resource, which returns a JSON response containing `{\\'hello\\': \\'world\\'}` when accessed via a GET request to the root URL (`/`).\\n\\n5. **Run the application**\\n\\nOpen your terminal, navigate to the directory containing the `app.py` file, and run the application:\\n\\n```\\npython app.py\\n```\\n\\nYou should see output similar to the following:\\n\\n```\\n * Serving Flask app \"app.py\"\\n * Environment: production\\n * Debug mode: on\\n * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\\n * Debugger is active!\\n * > Here are the URLs for individual routes:\\n * > * For information on the API, see http://flask.pocoo.org/docs/1.0/api/\\n * > * The debugger catches CPython exceptions and provides feedback on JavaScript errors\\n * > * The profiler helps you analyze the performance of your app\\n * > * The Werkzeug debugger helps you interrupt execution and inspect your code\\n * Serving Flask app \"app.py\"\\n * Environment: production\\n * Debug mode: on\\n * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\\n```\\n\\nNow you have a running Flask RESTful API! You can access it at `http://127.0.0.1:5000/` on your web browser or, more commonly, programmatically using tools like curl or Postman.\\n\\n6. **Expand your API (optional)**\\n\\nTo expand your RESTful API, you can define additional resources by creating new subclasses of the `Resource` class and registering their URLs in the `api.add_resource()` method.\\n\\nHere is an example of extending our `HelloWorld` API:\\n\\n```python\\nclass HelloUser(Resource):\\n def get(self, name):\\n return {\\'message\\': f\\'Hello, {name}!\\'}\\n\\napi.add_resource(HelloUser, \\'/hello/\\')\\n```\\n\\nThis will add a new endpoint `/hello/` which will accept `GET` requests to return a message saying hello to the provided name.\\n\\n7. **Test your API**\\n\\nYou can test your API using HTTP tools like curl, Postman, or by directly accessing your browser. Here\\'s an example of making a GET request using curl:\\n\\n```\\ncurl http://127.0.0.1:5000/\\ncurl http://127.0.0.1:5000/hello/John\\n```\\n\\nYou\\'ve now created a simple Flask RESTful API project. Remember to keep learning more about RESTful APIs and practice coding to get better at it. Good luck!'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.choices[0].message.content" ] } ], "metadata": { "kernelspec": { "display_name": "pydev", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }