openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.3.7

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_embeddings.py

83lines · modeblame

08b8179aDavid Schnurr2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
6
7import pytest
8
9from openai import OpenAI, AsyncOpenAI
10from tests.utils import assert_matches_type
11from openai.types import CreateEmbeddingResponse
12from openai._client import OpenAI, AsyncOpenAI
13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15api_key = "My API Key"
16
17
18class TestEmbeddings:
19strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
20loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
21parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
22
23@parametrize
24def test_method_create(self, client: OpenAI) -> None:
25embedding = client.embeddings.create(
26input="The quick brown fox jumped over the lazy dog",
27model="text-embedding-ada-002",
28)
29assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
30
31@parametrize
32def test_method_create_with_all_params(self, client: OpenAI) -> None:
33embedding = client.embeddings.create(
34input="The quick brown fox jumped over the lazy dog",
35model="text-embedding-ada-002",
36encoding_format="float",
37user="user-1234",
38)
39assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
40
41@parametrize
42def test_raw_response_create(self, client: OpenAI) -> None:
43response = client.embeddings.with_raw_response.create(
44input="The quick brown fox jumped over the lazy dog",
45model="text-embedding-ada-002",
46)
47assert response.http_request.headers.get("X-Stainless-Lang") == "python"
48embedding = response.parse()
49assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
50
51
52class TestAsyncEmbeddings:
53strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
54loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
55parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
56
57@parametrize
58async def test_method_create(self, client: AsyncOpenAI) -> None:
59embedding = await client.embeddings.create(
60input="The quick brown fox jumped over the lazy dog",
61model="text-embedding-ada-002",
62)
63assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
64
65@parametrize
66async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
67embedding = await client.embeddings.create(
68input="The quick brown fox jumped over the lazy dog",
69model="text-embedding-ada-002",
70encoding_format="float",
71user="user-1234",
72)
73assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
74
75@parametrize
76async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
77response = await client.embeddings.with_raw_response.create(
78input="The quick brown fox jumped over the lazy dog",
79model="text-embedding-ada-002",
80)
81assert response.http_request.headers.get("X-Stainless-Lang") == "python"
82embedding = response.parse()
83assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])