openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.11.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/test_embeddings.py

112lines · modeblame

08b8179aDavid Schnurr2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
08b8179aDavid Schnurr2 years ago7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.types import CreateEmbeddingResponse
13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15
16
17class TestEmbeddings:
98d779fbStainless Bot2 years ago18parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago19
20@parametrize
21def test_method_create(self, client: OpenAI) -> None:
22embedding = client.embeddings.create(
23input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago24model="text-embedding-3-small",
08b8179aDavid Schnurr2 years ago25)
26assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
27
28@parametrize
29def test_method_create_with_all_params(self, client: OpenAI) -> None:
30embedding = client.embeddings.create(
31input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago32model="text-embedding-3-small",
33dimensions=1,
08b8179aDavid Schnurr2 years ago34encoding_format="float",
35user="user-1234",
36)
37assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
38
39@parametrize
40def test_raw_response_create(self, client: OpenAI) -> None:
41response = client.embeddings.with_raw_response.create(
42input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago43model="text-embedding-3-small",
08b8179aDavid Schnurr2 years ago44)
86379b44Stainless Bot2 years ago45
46assert response.is_closed is True
08b8179aDavid Schnurr2 years ago47assert response.http_request.headers.get("X-Stainless-Lang") == "python"
48embedding = response.parse()
49assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
50
86379b44Stainless Bot2 years ago51@parametrize
52def test_streaming_response_create(self, client: OpenAI) -> None:
53with client.embeddings.with_streaming_response.create(
54input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago55model="text-embedding-3-small",
86379b44Stainless Bot2 years ago56) as response:
57assert not response.is_closed
58assert response.http_request.headers.get("X-Stainless-Lang") == "python"
59
60embedding = response.parse()
61assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
62
63assert cast(Any, response.is_closed) is True
64
08b8179aDavid Schnurr2 years ago65
66class TestAsyncEmbeddings:
98d779fbStainless Bot2 years ago67parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago68
69@parametrize
98d779fbStainless Bot2 years ago70async def test_method_create(self, async_client: AsyncOpenAI) -> None:
71embedding = await async_client.embeddings.create(
08b8179aDavid Schnurr2 years ago72input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago73model="text-embedding-3-small",
08b8179aDavid Schnurr2 years ago74)
75assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
76
77@parametrize
98d779fbStainless Bot2 years ago78async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
79embedding = await async_client.embeddings.create(
08b8179aDavid Schnurr2 years ago80input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago81model="text-embedding-3-small",
82dimensions=1,
08b8179aDavid Schnurr2 years ago83encoding_format="float",
84user="user-1234",
85)
86assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
87
88@parametrize
98d779fbStainless Bot2 years ago89async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
90response = await async_client.embeddings.with_raw_response.create(
08b8179aDavid Schnurr2 years ago91input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago92model="text-embedding-3-small",
08b8179aDavid Schnurr2 years ago93)
86379b44Stainless Bot2 years ago94
95assert response.is_closed is True
08b8179aDavid Schnurr2 years ago96assert response.http_request.headers.get("X-Stainless-Lang") == "python"
97embedding = response.parse()
98assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
86379b44Stainless Bot2 years ago99
100@parametrize
98d779fbStainless Bot2 years ago101async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
102async with async_client.embeddings.with_streaming_response.create(
86379b44Stainless Bot2 years ago103input="The quick brown fox jumped over the lazy dog",
20d97ef0Stainless Bot2 years ago104model="text-embedding-3-small",
86379b44Stainless Bot2 years ago105) as response:
106assert not response.is_closed
107assert response.http_request.headers.get("X-Stainless-Lang") == "python"
108
109embedding = await response.parse()
110assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
111
112assert cast(Any, response.is_closed) is True