Implement the /v1/embeddings interface of OpenAI by yourself

Implement the /v1/embeddings interface of OpenAI by yourself

0. Background

You need to pay for using OpenAI's API Key. In order to save costs, try to implement various interfaces of OpenAI yourself.

This article is mainly some code examples for implementing /v1/embeddingsand /v1/engines/{model_name}/embeddingsinterfaces.

1. Modify the .env file

add COHERE_API_KEY,

COHERE_API_KEY='abcdeOuJIC5scu0dB6TJW0CijNMDP5tHfu8u2xyz' # 此 key 无效

2. Modify the get_embedding method

async def get_embedding(payload: Dict[str, Any]):
    # print(f"payload: {payload}")
    cohere_payload = {"texts": payload["input"], "truncate": "END"}
    # print(f"cohere_payload: {cohere_payload}")
    # print(f"os.environ['COHERE_API_KEY']: {os.environ['COHERE_API_KEY']}")
    cohere_headers = {
        'Accept': 'application/json',
        'Authorization': 'Bearer ' + os.environ['COHERE_API_KEY'],
        'Content-Type': 'application/json',
    }
    # print(f"cohere_headers: {cohere_headers}")
    async with httpx.AsyncClient() as client:
        # https://docs.cohere.com/reference/embed
        response = await client.post(
            "https://api.cohere.ai/v1/embed",
            headers=cohere_headers,
            json=cohere_payload,
            timeout=WORKER_API_TIMEOUT,
        )
        # print(f"response: {response}")
        cohere_embeddings = response.json()
        # print(f"cohere_embeddings: {cohere_embeddings}")
        embedding = {"embedding": cohere_embeddings["embeddings"], "token_num": 1}
        return embedding

end!

Guess you like

Origin blog.csdn.net/engchina/article/details/132073654
Recommended