openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
15488ce07cb97535d8564e82dd5cda3481bc1a81

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_embeddings.py

110lines · modecode

1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
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:
18 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
19
20 @parametrize
21 def test_method_create(self, client: OpenAI) -> None:
22 embedding = client.embeddings.create(
23 input="The quick brown fox jumped over the lazy dog",
24 model="text-embedding-ada-002",
25 )
26 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
27
28 @parametrize
29 def test_method_create_with_all_params(self, client: OpenAI) -> None:
30 embedding = client.embeddings.create(
31 input="The quick brown fox jumped over the lazy dog",
32 model="text-embedding-ada-002",
33 encoding_format="float",
34 user="user-1234",
35 )
36 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
37
38 @parametrize
39 def test_raw_response_create(self, client: OpenAI) -> None:
40 response = client.embeddings.with_raw_response.create(
41 input="The quick brown fox jumped over the lazy dog",
42 model="text-embedding-ada-002",
43 )
44
45 assert response.is_closed is True
46 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
47 embedding = response.parse()
48 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
49
50 @parametrize
51 def test_streaming_response_create(self, client: OpenAI) -> None:
52 with client.embeddings.with_streaming_response.create(
53 input="The quick brown fox jumped over the lazy dog",
54 model="text-embedding-ada-002",
55 ) as response:
56 assert not response.is_closed
57 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
58
59 embedding = response.parse()
60 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
61
62 assert cast(Any, response.is_closed) is True
63
64
65class TestAsyncEmbeddings:
66 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
67
68 @parametrize
69 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
70 embedding = await async_client.embeddings.create(
71 input="The quick brown fox jumped over the lazy dog",
72 model="text-embedding-ada-002",
73 )
74 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
75
76 @parametrize
77 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
78 embedding = await async_client.embeddings.create(
79 input="The quick brown fox jumped over the lazy dog",
80 model="text-embedding-ada-002",
81 encoding_format="float",
82 user="user-1234",
83 )
84 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
85
86 @parametrize
87 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
88 response = await async_client.embeddings.with_raw_response.create(
89 input="The quick brown fox jumped over the lazy dog",
90 model="text-embedding-ada-002",
91 )
92
93 assert response.is_closed is True
94 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
95 embedding = response.parse()
96 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
97
98 @parametrize
99 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
100 async with async_client.embeddings.with_streaming_response.create(
101 input="The quick brown fox jumped over the lazy dog",
102 model="text-embedding-ada-002",
103 ) as response:
104 assert not response.is_closed
105 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
106
107 embedding = await response.parse()
108 assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
109
110 assert cast(Any, response.is_closed) is True
111