Loading
Loading
Azure OpenAI Service provides access to OpenAI models (GPT-4) within Azure's secure infrastructure.
import json
from dataclasses import dataclass, field
from typing import List, Dict, Optional
# โโโ ู
ุญุงูุงุฉ Azure OpenAI SDK โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@dataclass
class AzureOpenAIDeployment:
name: str
model: str
capacity: int # 1 capacity unit = 1000 TPM
mode: str = "Standard" # Standard, Provisioned
MODELS_PRICING = {
"gpt-4o": {"in": 0.005, "out": 0.015, "ctx": "128K"},
"gpt-4-turbo": {"in": 0.01, "out": 0.03, "ctx": "128K"},
"gpt-35-turbo": {"in": 0.0005, "out": 0.0015, "ctx": "16K"},
"text-embedding-3-large": {"in": 0.00013, "out": 0, "ctx": "8K"},
}
class AzureOpenAIClient:
"""ู
ุญุงูุงุฉ openai.AzureOpenAI SDK"""
def __init__(self, endpoint: str, api_version: str = "2024-02-01"):
self.endpoint = endpoint
self.api_version = api_version
self.deployments: Dict[str, AzureOpenAIDeployment] = {}
self._total_cost = 0.0
self._calls = 0
def create_deployment(self, name: str, model: str, capacity: int = 1):
d = AzureOpenAIDeployment(name, model, capacity)
self.deployments[name] = d
ctx = MODELS_PRICING.get(model, {}).get("ctx", "?")
print(f" โ
Deployment: {name} ({model}, ctx={ctx})")
return d
def chat(self, deployment: str, messages: List[Dict],
system: str = "", temperature: float = 0.7) -> Dict:
if deployment not in self.deployments:
raise ValueError(f"Deployment '{deployment}' ุบูุฑ ู
ูุฌูุฏ")
d = self.deployments[deployment]
mdl = MODELS_PRICING.get(d.model, {"in": 0.01, "out": 0.03})
# ู
ุญุงูุงุฉ ุงูุฑุฏ
last_msg = messages[-1].get("content", "") if messages else ""
response = f"[{d.model}] ุฑุฏูู ุนูู: {last_msg[:55]}..."
in_tok = int(sum(len(m.get("content","").split()) * 1.3 for m in messages))
out_tok = 60
cost = (in_tok / 1000) * mdl["in"] + (out_tok / 1000) * mdl["out"]
self._total_cost += cost
self._calls += 1
return {
"content": response,
"usage": {"prompt_tokens": in_tok, "completion_tokens": out_tok},
"cost_usd": round(cost, 7),
"model": d.model,
"deployment": deployment,
}
def embed(self, deployment: str, text: str) -> List[float]:
"""ุชูููุฏ Embedding"""
import math
# ู
ุญุงูุงุฉ embedding 8-dimensional
embed = [math.sin(i * hash(text) % 100 * 0.1) for i in range(8)]
mag = math.sqrt(sum(x**2 for x in embed))
return [x / mag for x in embed]
def cost_summary(self):
total_str = "$" + f"{self._total_cost:.6f}"
print(f"\n๐ ู
ูุฎุต ุงูุงุณุชุฎุฏุงู
:")
print(f" ุงูู
ูุงูู
ุงุช : {self._calls}")
print(f" ุงูุชูููุฉ : {total_str}")
# โโโ ุฅุนุฏุงุฏ Azure OpenAI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("๐ต Azure OpenAI Service:")
print("=" * 52)
client = AzureOpenAIClient(
endpoint="https://my-openai.openai.azure.com/",
api_version="2024-02-01",
)
print("\n1๏ธโฃ ุฅูุดุงุก Deployments:")
client.create_deployment("gpt4o-prod", "gpt-4o", capacity=10)
client.create_deployment("gpt35-fast", "gpt-35-turbo", capacity=50)
client.create_deployment("embed-large", "text-embedding-3-large", capacity=5)
# โโโ ุงุณุชุฎุฏุงู
Chat Completions โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print(f"\n2๏ธโฃ ุงุฎุชุจุงุฑ Chat Completions:")
convos = [
("gpt4o-prod", "ู
ุง ูู Azure OpenAIุ ุฃุฌุจ ูู ุฌู
ูุชูู"),
("gpt35-fast", "ุงุนุทูู ู
ุซุงูุงู ุนูู use case ูู Azure OpenAI"),
("gpt4o-prod", "ููู ุฃุฎุชุงุฑ ุจูู Standard ู Provisioned deploymentsุ"),
]
for dep, msg in convos:
resp = client.chat(dep, [{"role": "user", "content": msg}],
system="ุฃุฌุจ ุจุงูุนุฑุจูุฉ ุจุงุฎุชุตุงุฑ")
cost_s = "$" + f"{resp['cost_usd']:.7f}"
print(f"\n [{dep}] {msg[:50]}")
print(f" ๐ฌ {resp['content']}")
print(f" ๐ข {resp['usage']['prompt_tokens']}in+{resp['usage']['completion_tokens']}out | {cost_s}")
# โโโ Embeddings โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print(f"\n\n3๏ธโฃ Embeddings ููุจุญุซ ุงูุฏูุงูู:")
texts = ["Azure OpenAI ููู
ุคุณุณุงุช", "AWS Bedrock ูู Amazon", "GCP Vertex AI ูู Google"]
embeddings = {t: client.embed("embed-large", t) for t in texts}
import math
def cosine_sim(a, b):
dot = sum(x*y for x,y in zip(a,b))
norm = math.sqrt(sum(x**2 for x in a)) * math.sqrt(sum(x**2 for x in b))
return dot / norm if norm > 0 else 0.0
query = "ุฎุฏู
ุฉ AI ุณุญุงุจูุฉ"
q_emb = client.embed("embed-large", query)
print(f" ุงูุงุณุชุนูุงู
: '{query}'")
for text, emb in embeddings.items():
sim = cosine_sim(q_emb, emb)
bar = "โ" * int(sim * 10)
print(f" {sim:.3f} {bar} {text}")
client.cost_summary()
print(f"\nโ
Azure OpenAI Service ุฌุงูุฒ!")