# coding=utf-8
from embeddings.embedding import Embedding
import dashscope
from http import HTTPStatus
from dashscope import TextEmbedding


class Dashscopeembedding(Embedding):
    def __init__(self, **param):
        dashscope.api_key = param['api_key']
        self.cache = {}

    def getem(self, question):
        if question in self.cache.keys():
            return self.cache['question']
        resp = TextEmbedding.call(model=TextEmbedding.Models.text_embedding_v1,
                                  input=question,
                                  text_type='query')
        if resp.status_code == HTTPStatus.OK:
            em = resp['output']['embeddings'][0]['embedding']
            self.cache['question'] = em
            return em
