|
39 | 39 | "source": [ |
40 | 40 | "%pip install azure-ai-evaluation\n", |
41 | 41 | "%pip install promptflow-azure\n", |
42 | | - "%pip install azure-identity" |
| 42 | + "%pip install azure-identity\n", |
| 43 | + "%pip install --upgrade openai" |
43 | 44 | ] |
44 | 45 | }, |
45 | 46 | { |
|
56 | 57 | "outputs": [], |
57 | 58 | "source": [ |
58 | 59 | "from pprint import pprint\n", |
59 | | - "from openai import AzureOpenAI\n", |
60 | 60 | "import pandas as pd\n", |
61 | | - "from azure.identity import DefaultAzureCredential, get_bearer_token_provider" |
| 61 | + "from azure.identity import DefaultAzureCredential" |
62 | 62 | ] |
63 | 63 | }, |
64 | 64 | { |
|
89 | 89 | "outputs": [], |
90 | 90 | "source": [ |
91 | 91 | "azure_ai_project = {\n", |
92 | | - " \"subscription_id\": \"<your-subscription-id>\",\n", |
93 | | - " \"resource_group_name\": \"<your-resource-group-name>\",\n", |
94 | | - " \"project_name\": \"<your-project-name>\",\n", |
| 92 | + " \"subscription_id\": \"<subscription_id>\",\n", |
| 93 | + " \"resource_group_name\": \"<resource_group_name>\",\n", |
| 94 | + " \"project_name\": \"<project_name>\",\n", |
95 | 95 | "}" |
96 | 96 | ] |
97 | 97 | }, |
|
103 | 103 | "source": [ |
104 | 104 | "import os\n", |
105 | 105 | "\n", |
106 | | - "# Use the following code to set the environment variables if not already set. If set, you can skip this step.\n", |
| 106 | + "# Use the following code to set the environment variables if not already set. If set, you can skip this step. In addition, you should also set \"AZURE_OPENAI_ENDPOINT\" to the endpoint of your AzureOpenAI service.\n", |
107 | 107 | "\n", |
108 | | - "os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"<api version>\"\n", |
109 | | - "os.environ[\"AZURE_OPENAI_DEPLOYMENT\"] = \"<your-deployment>\"\n", |
110 | | - "os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"<your-endpoint>\"" |
| 108 | + "os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"<openai_api_version>\"\n", |
| 109 | + "os.environ[\"AZURE_OPENAI_DEPLOYMENT\"] = \"<openai_deployment>\"" |
111 | 110 | ] |
112 | 111 | }, |
113 | 112 | { |
|
143 | 142 | "metadata": {}, |
144 | 143 | "outputs": [], |
145 | 144 | "source": [ |
| 145 | + "import os\n", |
| 146 | + "\n", |
146 | 147 | "model_config = {\n", |
147 | 148 | " \"azure_endpoint\": os.environ.get(\"AZURE_OPENAI_ENDPOINT\"),\n", |
148 | 149 | " \"azure_deployment\": os.environ.get(\"AZURE_OPENAI_DEPLOYMENT\"),\n", |
149 | 150 | "}" |
150 | 151 | ] |
151 | 152 | }, |
152 | | - { |
153 | | - "cell_type": "code", |
154 | | - "execution_count": null, |
155 | | - "metadata": {}, |
156 | | - "outputs": [], |
157 | | - "source": [ |
158 | | - "from typing_extensions import Self\n", |
159 | | - "from typing import TypedDict\n", |
160 | | - "from promptflow.tracing import trace\n", |
161 | | - "\n", |
162 | | - "\n", |
163 | | - "class ModelEndpoint:\n", |
164 | | - " def __init__(self: Self, env: dict) -> str:\n", |
165 | | - " self.env = env\n", |
166 | | - "\n", |
167 | | - " class Response(TypedDict):\n", |
168 | | - " query: str\n", |
169 | | - " response: str\n", |
170 | | - "\n", |
171 | | - " @trace\n", |
172 | | - " def __call__(self: Self, query: str) -> Response:\n", |
173 | | - " token_provider = get_bearer_token_provider(\n", |
174 | | - " DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\"\n", |
175 | | - " )\n", |
176 | | - "\n", |
177 | | - " client = AzureOpenAI(\n", |
178 | | - " azure_endpoint=self.env[\"azure_endpoint\"],\n", |
179 | | - " api_version=\"2024-06-01\",\n", |
180 | | - " azure_ad_token_provider=token_provider,\n", |
181 | | - " )\n", |
182 | | - " # Call the model\n", |
183 | | - " completion = client.chat.completions.create(\n", |
184 | | - " model=self.env[\"azure_deployment\"],\n", |
185 | | - " messages=[\n", |
186 | | - " {\n", |
187 | | - " \"role\": \"user\",\n", |
188 | | - " \"content\": query,\n", |
189 | | - " }\n", |
190 | | - " ],\n", |
191 | | - " max_tokens=800,\n", |
192 | | - " temperature=0.7,\n", |
193 | | - " top_p=0.95,\n", |
194 | | - " frequency_penalty=0,\n", |
195 | | - " presence_penalty=0,\n", |
196 | | - " stop=None,\n", |
197 | | - " stream=False,\n", |
198 | | - " )\n", |
199 | | - " output = completion.to_dict()\n", |
200 | | - " return {\"query\": query, \"response\": output[\"choices\"][0][\"message\"][\"content\"]}" |
201 | | - ] |
202 | | - }, |
203 | 153 | { |
204 | 154 | "cell_type": "markdown", |
205 | 155 | "metadata": {}, |
|
236 | 186 | " FluencyEvaluator,\n", |
237 | 187 | " SimilarityEvaluator,\n", |
238 | 188 | ")\n", |
| 189 | + "from model_endpoint import ModelEndpoint\n", |
239 | 190 | "\n", |
240 | 191 | "\n", |
241 | | - "content_safety_evaluator = ContentSafetyEvaluator(azure_ai_project)\n", |
| 192 | + "content_safety_evaluator = ContentSafetyEvaluator(\n", |
| 193 | + " azure_ai_project=azure_ai_project, credential=DefaultAzureCredential()\n", |
| 194 | + ")\n", |
242 | 195 | "relevance_evaluator = RelevanceEvaluator(model_config)\n", |
243 | 196 | "coherence_evaluator = CoherenceEvaluator(model_config)\n", |
244 | 197 | "groundedness_evaluator = GroundednessEvaluator(model_config)\n", |
|
310 | 263 | ], |
311 | 264 | "metadata": { |
312 | 265 | "kernelspec": { |
313 | | - "display_name": "venv-azureai-samples", |
| 266 | + "display_name": ".venv", |
314 | 267 | "language": "python", |
315 | 268 | "name": "python3" |
316 | 269 | }, |
|
0 commit comments