openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.9.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_embeddings.py

110lines · 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",
24model="text-embedding-ada-002",
25)
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",
32model="text-embedding-ada-002",
33encoding_format="float",
34user="user-1234",
35)
36assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
37
38@parametrize
39def test_raw_response_create(self, client: OpenAI) -> None:
40response = client.embeddings.with_raw_response.create(
41input="The quick brown fox jumped over the lazy dog",
42model="text-embedding-ada-002",
43)
86379b44Stainless Bot2 years ago44
45assert response.is_closed is True
08b8179aDavid Schnurr2 years ago46assert response.http_request.headers.get("X-Stainless-Lang") == "python"
47embedding = response.parse()
48assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
49
86379b44Stainless Bot2 years ago50@parametrize
51def test_streaming_response_create(self, client: OpenAI) -> None:
52with client.embeddings.with_streaming_response.create(
53input="The quick brown fox jumped over the lazy dog",
54model="text-embedding-ada-002",
55) as response:
56assert not response.is_closed
57assert response.http_request.headers.get("X-Stainless-Lang") == "python"
58
59embedding = response.parse()
60assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
61
62assert cast(Any, response.is_closed) is True
63
08b8179aDavid Schnurr2 years ago64
65class TestAsyncEmbeddings:
98d779fbStainless Bot2 years ago66parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago67
68@parametrize
98d779fbStainless Bot2 years ago69async def test_method_create(self, async_client: AsyncOpenAI) -> None:
70embedding = await async_client.embeddings.create(
08b8179aDavid Schnurr2 years ago71input="The quick brown fox jumped over the lazy dog",
72model="text-embedding-ada-002",
73)
74assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
75
76@parametrize
98d779fbStainless Bot2 years ago77async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
78embedding = await async_client.embeddings.create(
08b8179aDavid Schnurr2 years ago79input="The quick brown fox jumped over the lazy dog",
80model="text-embedding-ada-002",
81encoding_format="float",
82user="user-1234",
83)
84assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
85
86@parametrize
98d779fbStainless Bot2 years ago87async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
88response = await async_client.embeddings.with_raw_response.create(
08b8179aDavid Schnurr2 years ago89input="The quick brown fox jumped over the lazy dog",
90model="text-embedding-ada-002",
91)
86379b44Stainless Bot2 years ago92
93assert response.is_closed is True
08b8179aDavid Schnurr2 years ago94assert response.http_request.headers.get("X-Stainless-Lang") == "python"
95embedding = response.parse()
96assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
86379b44Stainless Bot2 years ago97
98@parametrize
98d779fbStainless Bot2 years ago99async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
100async with async_client.embeddings.with_streaming_response.create(
86379b44Stainless Bot2 years ago101input="The quick brown fox jumped over the lazy dog",
102model="text-embedding-ada-002",
103) as response:
104assert not response.is_closed
105assert response.http_request.headers.get("X-Stainless-Lang") == "python"
106
107embedding = await response.parse()
108assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
109
110assert cast(Any, response.is_closed) is True