Skip to content

Commit 2bf307e

Browse files
authored
Fixed #173 - Evaluate Base Model Endpoints giving errors (#175)
Moved `ModelEndpoints` class to a new Python file.
1 parent f80d09d commit 2bf307e

File tree

2 files changed

+122
-120
lines changed

2 files changed

+122
-120
lines changed

scenarios/evaluate/Supported_Evaluation_Targets/Evaluate_Base_Model_Endpoint/Evaluate_Base_Model_Endpoint.ipynb

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -158,125 +158,7 @@
158158
"metadata": {},
159159
"outputs": [],
160160
"source": [
161-
"import requests\n",
162-
"from typing_extensions import Self\n",
163-
"from typing import TypedDict\n",
164-
"from promptflow.tracing import trace\n",
165-
"\n",
166-
"\n",
167-
"class ModelEndpoints:\n",
168-
" def __init__(self: Self, env: dict, model_type: str) -> str:\n",
169-
" self.env = env\n",
170-
" self.model_type = model_type\n",
171-
"\n",
172-
" class Response(TypedDict):\n",
173-
" query: str\n",
174-
" response: str\n",
175-
"\n",
176-
" @trace\n",
177-
" def __call__(self: Self, query: str) -> Response:\n",
178-
" if self.model_type == \"gpt4-0613\":\n",
179-
" output = self.call_gpt4_endpoint(query)\n",
180-
" elif self.model_type == \"gpt35-turbo\":\n",
181-
" output = self.call_gpt35_turbo_endpoint(query)\n",
182-
" elif self.model_type == \"mistral7b\":\n",
183-
" output = self.call_mistral_endpoint(query)\n",
184-
" elif self.model_type == \"tiny_llama\":\n",
185-
" output = self.call_tiny_llama_endpoint(query)\n",
186-
" elif self.model_type == \"phi3_mini_serverless\":\n",
187-
" output = self.call_phi3_mini_serverless_endpoint(query)\n",
188-
" elif self.model_type == \"gpt2\":\n",
189-
" output = self.call_gpt2_endpoint(query)\n",
190-
" else:\n",
191-
" output = self.call_default_endpoint(query)\n",
192-
"\n",
193-
" return output\n",
194-
"\n",
195-
" def query(self: Self, endpoint: str, headers: str, payload: str) -> str:\n",
196-
" response = requests.post(url=endpoint, headers=headers, json=payload)\n",
197-
" return response.json()\n",
198-
"\n",
199-
" def call_gpt4_endpoint(self: Self, query: str) -> Response:\n",
200-
" endpoint = self.env[\"gpt4-0613\"][\"endpoint\"]\n",
201-
" key = self.env[\"gpt4-0613\"][\"key\"]\n",
202-
"\n",
203-
" headers = {\"Content-Type\": \"application/json\", \"api-key\": key}\n",
204-
"\n",
205-
" payload = {\"messages\": [{\"role\": \"user\", \"content\": query}], \"max_tokens\": 500}\n",
206-
"\n",
207-
" output = self.query(endpoint=endpoint, headers=headers, payload=payload)\n",
208-
" response = output[\"choices\"][0][\"message\"][\"content\"]\n",
209-
" return {\"query\": query, \"response\": response}\n",
210-
"\n",
211-
" def call_gpt35_turbo_endpoint(self: Self, query: str) -> Response:\n",
212-
" endpoint = self.env[\"gpt35-turbo\"][\"endpoint\"]\n",
213-
" key = self.env[\"gpt35-turbo\"][\"key\"]\n",
214-
"\n",
215-
" headers = {\"Content-Type\": \"application/json\", \"api-key\": key}\n",
216-
"\n",
217-
" payload = {\"messages\": [{\"role\": \"user\", \"content\": query}], \"max_tokens\": 500}\n",
218-
"\n",
219-
" output = self.query(endpoint=endpoint, headers=headers, payload=payload)\n",
220-
" response = output[\"choices\"][0][\"message\"][\"content\"]\n",
221-
" return {\"query\": query, \"response\": response}\n",
222-
"\n",
223-
" def call_tiny_llama_endpoint(self: Self, query: str) -> Response:\n",
224-
" endpoint = self.env[\"tiny_llama\"][\"endpoint\"]\n",
225-
" key = self.env[\"tiny_llama\"][\"key\"]\n",
226-
"\n",
227-
" headers = {\"Content-Type\": \"application/json\", \"Authorization\": (\"Bearer \" + key)}\n",
228-
"\n",
229-
" payload = {\n",
230-
" \"model\": \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\",\n",
231-
" \"messages\": [{\"role\": \"user\", \"content\": query}],\n",
232-
" \"max_tokens\": 500,\n",
233-
" \"stream\": False,\n",
234-
" }\n",
235-
"\n",
236-
" output = self.query(endpoint=endpoint, headers=headers, payload=payload)\n",
237-
" response = output[\"choices\"][0][\"message\"][\"content\"]\n",
238-
" return {\"query\": query, \"response\": response}\n",
239-
"\n",
240-
" def call_phi3_mini_serverless_endpoint(self: Self, query: str) -> Response:\n",
241-
" endpoint = self.env[\"phi3_mini_serverless\"][\"endpoint\"]\n",
242-
" key = self.env[\"phi3_mini_serverless\"][\"key\"]\n",
243-
"\n",
244-
" headers = {\"Content-Type\": \"application/json\", \"Authorization\": (\"Bearer \" + key)}\n",
245-
"\n",
246-
" payload = {\"messages\": [{\"role\": \"user\", \"content\": query}], \"max_tokens\": 500}\n",
247-
"\n",
248-
" output = self.query(endpoint=endpoint, headers=headers, payload=payload)\n",
249-
" response = output[\"choices\"][0][\"message\"][\"content\"]\n",
250-
" return {\"query\": query, \"response\": response}\n",
251-
"\n",
252-
" def call_gpt2_endpoint(self: Self, query: str) -> Response:\n",
253-
" endpoint = self.env[\"gpt2\"][\"endpoint\"]\n",
254-
" key = self.env[\"gpt2\"][\"key\"]\n",
255-
"\n",
256-
" headers = {\"Content-Type\": \"application/json\", \"Authorization\": (\"Bearer \" + key)}\n",
257-
"\n",
258-
" payload = {\n",
259-
" \"inputs\": query,\n",
260-
" }\n",
261-
"\n",
262-
" output = self.query(endpoint=endpoint, headers=headers, payload=payload)\n",
263-
" response = output[0][\"generated_text\"]\n",
264-
" return {\"query\": query, \"response\": response}\n",
265-
"\n",
266-
" def call_mistral_endpoint(self: Self, query: str) -> Response:\n",
267-
" endpoint = self.env[\"mistral7b\"][\"endpoint\"]\n",
268-
" key = self.env[\"mistral7b\"][\"key\"]\n",
269-
"\n",
270-
" headers = {\"Content-Type\": \"application/json\", \"Authorization\": (\"Bearer \" + key)}\n",
271-
"\n",
272-
" payload = {\"messages\": [{\"content\": query, \"role\": \"user\"}], \"max_tokens\": 50}\n",
273-
"\n",
274-
" output = self.query(endpoint=endpoint, headers=headers, payload=payload)\n",
275-
" response = output[\"choices\"][0][\"message\"][\"content\"]\n",
276-
" return {\"query\": query, \"response\": response}\n",
277-
"\n",
278-
" def call_default_endpoint(query: str) -> Response:\n",
279-
" return {\"query\": \"What is the capital of France?\", \"response\": \"Paris\"}"
161+
"!pygmentize model_endpoints.py"
280162
]
281163
},
282164
{
@@ -349,6 +231,7 @@
349231
"from azure.ai.evaluation import (\n",
350232
" RelevanceEvaluator,\n",
351233
")\n",
234+
"from model_endpoints import ModelEndpoints\n",
352235
"\n",
353236
"relevance_evaluator = RelevanceEvaluator(model_config)\n",
354237
"\n",
@@ -412,7 +295,7 @@
412295
],
413296
"metadata": {
414297
"kernelspec": {
415-
"display_name": "venv-azureai-samples",
298+
"display_name": ".venv",
416299
"language": "python",
417300
"name": "python3"
418301
},
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import requests
2+
from typing_extensions import Self
3+
from typing import TypedDict
4+
from promptflow.tracing import trace
5+
6+
7+
class ModelEndpoints:
8+
def __init__(self: Self, env: dict, model_type: str) -> str:
9+
self.env = env
10+
self.model_type = model_type
11+
12+
class Response(TypedDict):
13+
query: str
14+
response: str
15+
16+
@trace
17+
def __call__(self: Self, query: str) -> Response:
18+
if self.model_type == "gpt4-0821":
19+
output = self.call_gpt4_endpoint(query)
20+
elif self.model_type == "gpt35-turbo":
21+
output = self.call_gpt35_turbo_endpoint(query)
22+
elif self.model_type == "mistral7b":
23+
output = self.call_mistral_endpoint(query)
24+
elif self.model_type == "tiny_llama":
25+
output = self.call_tiny_llama_endpoint(query)
26+
elif self.model_type == "phi3_mini_serverless":
27+
output = self.call_phi3_mini_serverless_endpoint(query)
28+
elif self.model_type == "gpt2":
29+
output = self.call_gpt2_endpoint(query)
30+
else:
31+
output = self.call_default_endpoint(query)
32+
33+
return output
34+
35+
def query(self: Self, endpoint: str, headers: str, payload: str) -> str:
36+
response = requests.post(url=endpoint, headers=headers, json=payload)
37+
return response.json()
38+
39+
def call_gpt4_endpoint(self: Self, query: str) -> Response:
40+
endpoint = self.env["gpt4-0821"]["endpoint"]
41+
key = self.env["gpt4-0821"]["key"]
42+
43+
headers = {"Content-Type": "application/json", "api-key": key}
44+
45+
payload = {"messages": [{"role": "user", "content": query}], "max_tokens": 500}
46+
47+
output = self.query(endpoint=endpoint, headers=headers, payload=payload)
48+
response = output["choices"][0]["message"]["content"]
49+
return {"query": query, "response": response}
50+
51+
def call_gpt35_turbo_endpoint(self: Self, query: str) -> Response:
52+
endpoint = self.env["gpt35-turbo"]["endpoint"]
53+
key = self.env["gpt35-turbo"]["key"]
54+
55+
headers = {"Content-Type": "application/json", "api-key": key}
56+
57+
payload = {"messages": [{"role": "user", "content": query}], "max_tokens": 500}
58+
59+
output = self.query(endpoint=endpoint, headers=headers, payload=payload)
60+
response = output["choices"][0]["message"]["content"]
61+
return {"query": query, "response": response}
62+
63+
def call_tiny_llama_endpoint(self: Self, query: str) -> Response:
64+
endpoint = self.env["tiny_llama"]["endpoint"]
65+
key = self.env["tiny_llama"]["key"]
66+
67+
headers = {"Content-Type": "application/json", "Authorization": ("Bearer " + key)}
68+
69+
payload = {
70+
"model": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
71+
"messages": [{"role": "user", "content": query}],
72+
"max_tokens": 500,
73+
"stream": False,
74+
}
75+
76+
output = self.query(endpoint=endpoint, headers=headers, payload=payload)
77+
response = output["choices"][0]["message"]["content"]
78+
return {"query": query, "response": response}
79+
80+
def call_phi3_mini_serverless_endpoint(self: Self, query: str) -> Response:
81+
endpoint = self.env["phi3_mini_serverless"]["endpoint"]
82+
key = self.env["phi3_mini_serverless"]["key"]
83+
84+
headers = {"Content-Type": "application/json", "Authorization": ("Bearer " + key)}
85+
86+
payload = {"messages": [{"role": "user", "content": query}], "max_tokens": 500}
87+
88+
output = self.query(endpoint=endpoint, headers=headers, payload=payload)
89+
response = output["choices"][0]["message"]["content"]
90+
return {"query": query, "response": response}
91+
92+
def call_gpt2_endpoint(self: Self, query: str) -> Response:
93+
endpoint = self.env["gpt2"]["endpoint"]
94+
key = self.env["gpt2"]["key"]
95+
96+
headers = {"Content-Type": "application/json", "Authorization": ("Bearer " + key)}
97+
98+
payload = {
99+
"inputs": query,
100+
}
101+
102+
output = self.query(endpoint=endpoint, headers=headers, payload=payload)
103+
response = output[0]["generated_text"]
104+
return {"query": query, "response": response}
105+
106+
def call_mistral_endpoint(self: Self, query: str) -> Response:
107+
endpoint = self.env["mistral7b"]["endpoint"]
108+
key = self.env["mistral7b"]["key"]
109+
110+
headers = {"Content-Type": "application/json", "Authorization": ("Bearer " + key)}
111+
112+
payload = {"messages": [{"content": query, "role": "user"}], "max_tokens": 50}
113+
114+
output = self.query(endpoint=endpoint, headers=headers, payload=payload)
115+
response = output["choices"][0]["message"]["content"]
116+
return {"query": query, "response": response}
117+
118+
def call_default_endpoint(query: str) -> Response:
119+
return {"query": "What is the capital of France?", "response": "Paris"}

0 commit comments

Comments
 (0)