openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_embeddings.py

116lines · 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
13from openai._client import OpenAI, AsyncOpenAI
14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16api_key = "My API Key"
17
18
19class TestEmbeddings:
20strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
21loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
22parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
23
24@parametrize
25def test_method_create(self, client: OpenAI) -> None:
26embedding = client.embeddings.create(
27input="The quick brown fox jumped over the lazy dog",
28model="text-embedding-ada-002",
29)
30assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
31
32@parametrize
33def test_method_create_with_all_params(self, client: OpenAI) -> None:
34embedding = client.embeddings.create(
35input="The quick brown fox jumped over the lazy dog",
36model="text-embedding-ada-002",
37encoding_format="float",
38user="user-1234",
39)
40assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
41
42@parametrize
43def test_raw_response_create(self, client: OpenAI) -> None:
44response = client.embeddings.with_raw_response.create(
45input="The quick brown fox jumped over the lazy dog",
46model="text-embedding-ada-002",
47)
86379b44Stainless Bot2 years ago48
49assert response.is_closed is True
08b8179aDavid Schnurr2 years ago50assert response.http_request.headers.get("X-Stainless-Lang") == "python"
51embedding = response.parse()
52assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
53
86379b44Stainless Bot2 years ago54@parametrize
55def test_streaming_response_create(self, client: OpenAI) -> None:
56with client.embeddings.with_streaming_response.create(
57input="The quick brown fox jumped over the lazy dog",
58model="text-embedding-ada-002",
59) as response:
60assert not response.is_closed
61assert response.http_request.headers.get("X-Stainless-Lang") == "python"
62
63embedding = response.parse()
64assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
65
66assert cast(Any, response.is_closed) is True
67
08b8179aDavid Schnurr2 years ago68
69class TestAsyncEmbeddings:
70strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
71loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
72parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
73
74@parametrize
75async def test_method_create(self, client: AsyncOpenAI) -> None:
76embedding = await client.embeddings.create(
77input="The quick brown fox jumped over the lazy dog",
78model="text-embedding-ada-002",
79)
80assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
81
82@parametrize
83async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
84embedding = await client.embeddings.create(
85input="The quick brown fox jumped over the lazy dog",
86model="text-embedding-ada-002",
87encoding_format="float",
88user="user-1234",
89)
90assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
91
92@parametrize
93async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
94response = await client.embeddings.with_raw_response.create(
95input="The quick brown fox jumped over the lazy dog",
96model="text-embedding-ada-002",
97)
86379b44Stainless Bot2 years ago98
99assert response.is_closed is True
08b8179aDavid Schnurr2 years ago100assert response.http_request.headers.get("X-Stainless-Lang") == "python"
101embedding = response.parse()
102assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
86379b44Stainless Bot2 years ago103
104@parametrize
105async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
106async with client.embeddings.with_streaming_response.create(
107input="The quick brown fox jumped over the lazy dog",
108model="text-embedding-ada-002",
109) as response:
110assert not response.is_closed
111assert response.http_request.headers.get("X-Stainless-Lang") == "python"
112
113embedding = await response.parse()
114assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
115
116assert cast(Any, response.is_closed) is True