davidkhala.ai 0.0.0__py3-none-any.whl → 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
File without changes
@@ -0,0 +1,68 @@
1
+ from enum import Enum
2
+ from http import HTTPStatus
3
+ from typing import List
4
+
5
+ from dashscope import Generation, TextEmbedding
6
+ from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
7
+
8
+ from davidkhala.ai.model import AbstractClient
9
+
10
+
11
+ class ModelEnum(str, Enum):
12
+ BAILIAN = Generation.Models.bailian_v1
13
+ DOLLY = Generation.Models.dolly_12b_v2
14
+ TURBO = Generation.Models.qwen_turbo
15
+ PLUS = Generation.Models.qwen_plus
16
+ MAX = Generation.Models.qwen_max
17
+ EMBED = TextEmbedding.Models.text_embedding_v4
18
+
19
+
20
+ class API(AbstractClient):
21
+ """
22
+ Unsupported to use international base_url "https://dashscope-intl.aliyuncs.com"
23
+ """
24
+
25
+ model: ModelEnum
26
+
27
+ def __init__(self, api_key):
28
+ self.api_key = api_key
29
+
30
+ def as_embeddings(self, model=ModelEnum.EMBED):
31
+ super().as_embeddings(model)
32
+
33
+ @staticmethod
34
+ def _on_response(response:DashScopeAPIResponse):
35
+ if response.status_code == HTTPStatus.OK:
36
+ return response.output
37
+ else:
38
+ raise Exception(response)
39
+
40
+
41
+ def chat(self, user_prompt: str, **kwargs):
42
+
43
+ if not self.messages:
44
+ kwargs['prompt'] = user_prompt
45
+ else:
46
+ kwargs['messages'] = [
47
+ *self.messages,
48
+ {
49
+ "role": "user",
50
+ 'content': user_prompt
51
+ }
52
+ ]
53
+ # prompt 和 messages 是互斥的参数:如果你使用了 messages,就不要再传 prompt
54
+ r = Generation.call(
55
+ self.model,
56
+ api_key=self.api_key,
57
+ **kwargs
58
+ )
59
+ return API._on_response(r)
60
+
61
+ def encode(self, *_input: str)-> List[List[float]]:
62
+ r= TextEmbedding.call(
63
+ self.model,list(_input),
64
+ api_key= self.api_key,
65
+ )
66
+ r = API._on_response(r)
67
+
68
+ return [item['embedding'] for item in r['embeddings']]
davidkhala/ai/model.py ADDED
@@ -0,0 +1,28 @@
1
+ from abc import ABC
2
+ from typing import Optional, List
3
+
4
+
5
+ class AbstractClient(ABC):
6
+ api_key: str
7
+ base_url: str
8
+ model: Optional[str]
9
+ messages = []
10
+
11
+ def as_chat(self, model: str, sys_prompt: str = None):
12
+ self.model = model
13
+ if sys_prompt is not None:
14
+ self.messages = [{"role": "system", "content": sys_prompt}]
15
+
16
+ def as_embeddings(self, model: str):
17
+ self.model = model
18
+
19
+ def chat(self, user_prompt: str, **kwargs):
20
+ ...
21
+
22
+ def encode(self, *_input: str) -> List[List[float]]:
23
+ ...
24
+ def connect(self):
25
+ ...
26
+
27
+ def disconnect(self):
28
+ ...
@@ -1,35 +1,25 @@
1
1
  import runpy
2
- from abc import ABC
3
- from typing import Union, Optional, Literal, List
2
+ from typing import Union, Literal, List
4
3
 
5
4
  from openai import OpenAI, AsyncOpenAI
6
5
 
6
+ from davidkhala.ai.model import AbstractClient
7
7
 
8
- class Client(ABC):
9
- api_key: str
10
- base_url: str
11
- model: Optional[str]
12
- messages = []
13
- client: OpenAI
14
-
15
- def as_chat(self, model, sys_prompt: str = None):
16
- self.model = model
17
- if sys_prompt is not None:
18
- self.messages = [{"role": "system", "content": sys_prompt}]
19
8
 
20
- def as_embeddings(self, model, encoding_format: str = "float"):
21
- self.model = model
9
+ class Client(AbstractClient):
10
+ client: OpenAI
11
+ encoding_format: Literal["float", "base64"] = "float"
22
12
 
23
13
  def connect(self):
24
14
  self.client.models.list()
25
15
 
26
- def encode(self, _input: str, _format: Literal["float", "base64"] = "float")-> List[float]:
16
+ def encode(self, *_input: str) -> List[List[float]]:
27
17
  response = self.client.embeddings.create(
28
18
  model=self.model,
29
- input=_input,
30
- encoding_format=_format
19
+ input=list(_input),
20
+ encoding_format=self.encoding_format
31
21
  )
32
- return response.data[0].embedding
22
+ return [item.embedding for item in response.data]
33
23
 
34
24
  def chat(self, user_prompt, image: str = None):
35
25
 
@@ -1,9 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: davidkhala.ai
3
- Version: 0.0.0
3
+ Version: 0.0.2
4
4
  Summary: misc AI modules
5
- Requires-Python: >=3.13
5
+ Requires-Python: ==3.13.*
6
6
  Requires-Dist: opik
7
+ Provides-Extra: ali
8
+ Requires-Dist: dashscope; extra == 'ali'
7
9
  Provides-Extra: api
8
10
  Requires-Dist: requests; extra == 'api'
9
11
  Provides-Extra: google
@@ -1,16 +1,20 @@
1
1
  davidkhala/ai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ davidkhala/ai/model.py,sha256=ujc88wQUP7SAyg9K8SQ45AuFOPFGtwAUyL05jYlUEBQ,657
2
3
  davidkhala/ai/opik.py,sha256=YU1XuweMUAzUkhpjxhltt-SBBDBkR3z-PCNo0DqzBRs,39
3
4
  davidkhala/ai/agent/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ davidkhala/ai/agent/dify.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
6
  davidkhala/ai/agent/langgraph.py,sha256=VEA2NQov_-KffTjgzO7nOIHiI4U9Y6enLTe3MQqKihc,1121
7
+ davidkhala/ai/ali/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ davidkhala/ai/ali/dashscope.py,sha256=b49unSTVXhjHCA0F1Qt6_-Zs7zjSgEetIcVK5E2nDMQ,1968
5
9
  davidkhala/ai/api/__init__.py,sha256=IKT7TmMUrfk6bllpsXdBd-w13lHgWADU5Qk5ln01DLY,2018
6
10
  davidkhala/ai/api/open.py,sha256=_63n7Trrvj5pK-ZCud3HIJ-cpZWDAPpCQUjXGusFdU0,1221
7
11
  davidkhala/ai/api/siliconflow.py,sha256=uts5K1TpBOg8Eo6UZkNJWktQPoolfmn2sID01wiac6s,1314
8
12
  davidkhala/ai/google/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
13
  davidkhala/ai/google/adk.py,sha256=QwxYoOzT2Hol03V4NM0PF_HAzUGb4fB18VUAYacYbAY,657
10
14
  davidkhala/ai/google/gemini.py,sha256=Xf4HDOOcK4-jEBERzuLnQNFsU61P2fFx4K0z-ijvNHE,214
11
- davidkhala/ai/openai/__init__.py,sha256=GrgaO2NEoJKEWMc20yBbTx_-YikA0zRjJbtn0_j9RJs,1887
15
+ davidkhala/ai/openai/__init__.py,sha256=SvQvUgVgw5vRQtF4S3WUYqg1DOzGSQ7BtHvTSEwkmpY,1597
12
16
  davidkhala/ai/openai/azure.py,sha256=jAtZNW1d8ub3odhB_f1MXxM7MImg64MXDnqLm1-Idkk,828
13
17
  davidkhala/ai/openai/native.py,sha256=-XuHn0KSQnuFdJ4jTwZla1ts5iOW2L2Hosfo8o8iip8,257
14
- davidkhala_ai-0.0.0.dist-info/METADATA,sha256=hQG-59SIYeqz3YkWsapC3sFC4dBolcTtksG9n___Msw,539
15
- davidkhala_ai-0.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- davidkhala_ai-0.0.0.dist-info/RECORD,,
18
+ davidkhala_ai-0.0.2.dist-info/METADATA,sha256=rR3VkQp9l3KG5QVtiLIYZHHWVvwH4eoYBIdaa4p3wlQ,602
19
+ davidkhala_ai-0.0.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
20
+ davidkhala_ai-0.0.2.dist-info/RECORD,,