File size: 4,684 Bytes
ea8c728 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import asyncio
from types import SimpleNamespace
import openai
from shinka.embed.client import get_async_client_embed, get_client_embed
from shinka.embed.embedding import AsyncEmbeddingClient, EmbeddingClient
def test_get_client_embed_dynamic_openrouter(monkeypatch):
monkeypatch.setenv("OPENROUTER_API_KEY", "test-openrouter-key")
client, model_name = get_client_embed("openrouter/qwen/qwen3-coder")
assert model_name == "qwen/qwen3-coder"
assert "openrouter.ai" in str(client.base_url)
def test_get_async_client_embed_dynamic_openrouter(monkeypatch):
monkeypatch.setenv("OPENROUTER_API_KEY", "test-openrouter-key")
client, model_name = get_async_client_embed("openrouter/qwen/qwen3-coder")
assert isinstance(client, openai.AsyncOpenAI)
assert model_name == "qwen/qwen3-coder"
assert "openrouter.ai" in str(client.base_url)
def test_get_client_embed_local_openai_inline_url(monkeypatch):
monkeypatch.delenv("LOCAL_OPENAI_API_KEY", raising=False)
client, model_name = get_client_embed(
"local/BAAI/bge-small-en-v1.5@http://localhost:8080/v1"
)
assert model_name == "BAAI/bge-small-en-v1.5"
assert str(client.base_url).startswith("http://localhost:8080")
def test_get_async_client_embed_local_openai_inline_url(monkeypatch):
monkeypatch.setenv("LOCAL_OPENAI_API_KEY", "test-local-key")
client, model_name = get_async_client_embed(
"local/BAAI/bge-small-en-v1.5@http://localhost:8080/v1"
)
assert isinstance(client, openai.AsyncOpenAI)
assert model_name == "BAAI/bge-small-en-v1.5"
assert str(client.base_url).startswith("http://localhost:8080")
def test_sync_openrouter_embedding_unknown_price_defaults_to_zero(monkeypatch):
fake_response = SimpleNamespace(
data=[SimpleNamespace(embedding=[0.1, 0.2, 0.3])],
usage=SimpleNamespace(total_tokens=7),
)
fake_client = SimpleNamespace(
embeddings=SimpleNamespace(
create=lambda **kwargs: fake_response,
)
)
monkeypatch.setattr(
"shinka.embed.embedding.get_client_embed",
lambda model_name: (fake_client, "qwen/qwen3-coder"),
)
client = EmbeddingClient(model_name="openrouter/qwen/qwen3-coder")
embedding, cost = client.get_embedding("one two")
assert embedding == [0.1, 0.2, 0.3]
assert cost == 0.0
def test_async_openrouter_embedding_unknown_price_defaults_to_zero(monkeypatch):
fake_response = SimpleNamespace(
data=[SimpleNamespace(embedding=[0.1, 0.2, 0.3])],
usage=SimpleNamespace(total_tokens=7),
)
async def create(**kwargs):
return fake_response
fake_client = SimpleNamespace(
embeddings=SimpleNamespace(
create=create,
)
)
monkeypatch.setattr(
"shinka.embed.embedding.get_async_client_embed",
lambda model_name: (fake_client, "qwen/qwen3-coder"),
)
client = AsyncEmbeddingClient(model_name="openrouter/qwen/qwen3-coder")
embedding, cost = asyncio.run(client.embed_async("one two"))
assert embedding == [0.1, 0.2, 0.3]
assert cost == 0.0
def test_sync_local_embedding_unknown_price_defaults_to_zero(monkeypatch):
fake_response = SimpleNamespace(
data=[SimpleNamespace(embedding=[0.1, 0.2, 0.3])],
usage=SimpleNamespace(total_tokens=7),
)
fake_client = SimpleNamespace(
embeddings=SimpleNamespace(
create=lambda **kwargs: fake_response,
)
)
monkeypatch.setattr(
"shinka.embed.embedding.get_client_embed",
lambda model_name: (fake_client, "BAAI/bge-small-en-v1.5"),
)
client = EmbeddingClient(
model_name="local/BAAI/bge-small-en-v1.5@http://localhost:8080/v1"
)
embedding, cost = client.get_embedding("one two")
assert embedding == [0.1, 0.2, 0.3]
assert cost == 0.0
def test_async_local_embedding_unknown_price_defaults_to_zero(monkeypatch):
fake_response = SimpleNamespace(
data=[SimpleNamespace(embedding=[0.1, 0.2, 0.3])],
usage=SimpleNamespace(total_tokens=7),
)
async def create(**kwargs):
return fake_response
fake_client = SimpleNamespace(
embeddings=SimpleNamespace(
create=create,
)
)
monkeypatch.setattr(
"shinka.embed.embedding.get_async_client_embed",
lambda model_name: (fake_client, "BAAI/bge-small-en-v1.5"),
)
client = AsyncEmbeddingClient(
model_name="local/BAAI/bge-small-en-v1.5@http://localhost:8080/v1"
)
embedding, cost = asyncio.run(client.embed_async("one two"))
assert embedding == [0.1, 0.2, 0.3]
assert cost == 0.0
|