livekit-plugins-anthropic 0.0.1__tar.gz

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.
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.1
2
+ Name: livekit-plugins-anthropic
3
+ Version: 0.0.1
4
+ Summary: LiveKit Python Plugins for Anthropic Models and Services
5
+ Home-page: https://github.com/livekit/python-agents
6
+ License: Apache-2.0
7
+ Project-URL: Documentation, https://docs.livekit.io
8
+ Project-URL: Website, https://livekit.io/
9
+ Project-URL: Source, https://github.com/livekit/python-agents
10
+ Keywords: webrtc,realtime,audio,video,livekit
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Topic :: Multimedia :: Sound/Audio
14
+ Classifier: Topic :: Multimedia :: Video
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.7
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3 :: Only
22
+ Requires-Python: >=3.7.0
23
+ Description-Content-Type: text/markdown
24
+ Requires-Dist: anthropic>=0.7.5
25
+
26
+ # LiveKit Pluginss for OpenAI Models and Services
@@ -0,0 +1 @@
1
+ # LiveKit Pluginss for OpenAI Models and Services
@@ -0,0 +1,15 @@
1
+ # Copyright 2023 LiveKit, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from .claude import ClaudePlugin, ClaudeMessage, ClaudeMessageRole
@@ -0,0 +1,126 @@
1
+ # Copyright 2023 LiveKit, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import os
16
+ import logging
17
+ import asyncio
18
+ from dataclasses import dataclass
19
+ from typing import AsyncIterable
20
+ from anthropic import AsyncAnthropic, HUMAN_PROMPT, AI_PROMPT
21
+ from enum import Enum
22
+
23
+ ClaudeMessageRole = Enum(
24
+ 'MessageRole', ["system", "human", "assistant"])
25
+
26
+
27
+ @dataclass
28
+ class ClaudeMessage:
29
+ role: ClaudeMessageRole
30
+ content: str
31
+
32
+ def to_api(self):
33
+ if ClaudeMessageRole.system == self.role:
34
+ return f"{self.content}"
35
+ elif ClaudeMessageRole.human == self.role:
36
+ return f"{HUMAN_PROMPT} {self.content}"
37
+ elif ClaudeMessageRole.assistant == self.role:
38
+ return f"{AI_PROMPT} {self.content}"
39
+ else:
40
+ raise ValueError("Invalid message role")
41
+
42
+
43
+ class ClaudePlugin:
44
+ def __init__(self, model: str = 'claude-2', system_message: str = ''):
45
+ self._client = AsyncAnthropic(
46
+ api_key=os.environ["ANTHROPIC_API_KEY"])
47
+ self._model = model
48
+ self._system_message = system_message
49
+ self._messages: [ClaudeMessage] = []
50
+ self._producing_response = False
51
+ self._needs_interrupt = False
52
+
53
+ def interrupt(self):
54
+ if self._producing_response:
55
+ self._needs_interrupt = True
56
+
57
+ async def close(self):
58
+ pass
59
+
60
+ async def add_message(self, message: ClaudeMessage) -> AsyncIterable[str]:
61
+ self._messages.append(message)
62
+ async for text in self._generate_text_streamed():
63
+ yield text
64
+
65
+ async def _generate_text_streamed(self) -> AsyncIterable[str]:
66
+ system_message = ClaudeMessage(
67
+ role=ClaudeMessageRole.system, content=self._system_message)
68
+
69
+ try:
70
+ '''
71
+ Example Claude2 formatting for prompts:
72
+
73
+ Cats are wonderful animals and loved by everyone, no matter how many legs they have.
74
+
75
+ Human: I have two pet cats. One of them is missing a leg. The other one has a normal number of legs for a cat to have. In total, how many legs do my cats have?
76
+
77
+ Assistant: Can I think step-by-step?
78
+
79
+ Human: Yes, please do.
80
+
81
+ Assistant:
82
+ '''
83
+ prompt = ''.join([system_message.to_api()] + [m.to_api()
84
+ for m in self._messages] + [ClaudeMessage(role=ClaudeMessageRole.assistant, content="").to_api()])
85
+ chat_stream = await asyncio.wait_for(
86
+ self._client.completions.create(
87
+ model=self._model,
88
+ max_tokens_to_sample=300,
89
+ stream=True,
90
+ prompt=prompt
91
+ ),
92
+ 10
93
+ )
94
+ except TimeoutError:
95
+ yield "Sorry, I'm taking too long to respond. Please try again later."
96
+ return
97
+
98
+ self._producing_response = True
99
+ full_response = ""
100
+
101
+ while True:
102
+ try:
103
+ chunk = await asyncio.wait_for(anext(chat_stream, None), 5)
104
+ except TimeoutError:
105
+ break
106
+ except asyncio.CancelledError:
107
+ self._producing_response = False
108
+ self._needs_interrupt = False
109
+ break
110
+
111
+ if chunk is None:
112
+ break
113
+ content = chunk.completion
114
+
115
+ if self._needs_interrupt:
116
+ self._needs_interrupt = False
117
+ logging.info("Claude interrupted")
118
+ break
119
+
120
+ if content is not None:
121
+ full_response += content
122
+ yield content
123
+
124
+ self._messages.append(ClaudeMessage(
125
+ role=ClaudeMessageRole.assistant, content=full_response))
126
+ self._producing_response = False
@@ -0,0 +1,15 @@
1
+ # Copyright 2023 LiveKit, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ __version__ = "0.0.1"
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.1
2
+ Name: livekit-plugins-anthropic
3
+ Version: 0.0.1
4
+ Summary: LiveKit Python Plugins for Anthropic Models and Services
5
+ Home-page: https://github.com/livekit/python-agents
6
+ License: Apache-2.0
7
+ Project-URL: Documentation, https://docs.livekit.io
8
+ Project-URL: Website, https://livekit.io/
9
+ Project-URL: Source, https://github.com/livekit/python-agents
10
+ Keywords: webrtc,realtime,audio,video,livekit
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Topic :: Multimedia :: Sound/Audio
14
+ Classifier: Topic :: Multimedia :: Video
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.7
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3 :: Only
22
+ Requires-Python: >=3.7.0
23
+ Description-Content-Type: text/markdown
24
+ Requires-Dist: anthropic>=0.7.5
25
+
26
+ # LiveKit Pluginss for OpenAI Models and Services
@@ -0,0 +1,11 @@
1
+ README.md
2
+ pyproject.toml
3
+ setup.py
4
+ livekit/plugins/anthropic/__init__.py
5
+ livekit/plugins/anthropic/claude.py
6
+ livekit/plugins/anthropic/version.py
7
+ livekit_plugins_anthropic.egg-info/PKG-INFO
8
+ livekit_plugins_anthropic.egg-info/SOURCES.txt
9
+ livekit_plugins_anthropic.egg-info/dependency_links.txt
10
+ livekit_plugins_anthropic.egg-info/requires.txt
11
+ livekit_plugins_anthropic.egg-info/top_level.txt
@@ -0,0 +1,3 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,63 @@
1
+ # Copyright 2023 LiveKit, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import os
16
+ import pathlib
17
+
18
+ import setuptools
19
+ import setuptools.command.build_py
20
+
21
+
22
+ here = pathlib.Path(__file__).parent.resolve()
23
+ about = {}
24
+ with open(os.path.join(here, 'livekit', 'plugins', 'anthropic', 'version.py'), 'r') as f:
25
+ exec(f.read(), about)
26
+
27
+
28
+ setuptools.setup(
29
+ name="livekit-plugins-anthropic",
30
+ version=about['__version__'],
31
+ description="LiveKit Python Plugins for Anthropic Models and Services",
32
+ long_description=(here / "README.md").read_text(encoding="utf-8"),
33
+ long_description_content_type="text/markdown",
34
+ url="https://github.com/livekit/python-agents",
35
+ cmdclass={},
36
+ classifiers=[
37
+ "Intended Audience :: Developers",
38
+ "License :: OSI Approved :: Apache Software License",
39
+ "Topic :: Multimedia :: Sound/Audio",
40
+ "Topic :: Multimedia :: Video",
41
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
42
+ "Programming Language :: Python :: 3",
43
+ "Programming Language :: Python :: 3.7",
44
+ "Programming Language :: Python :: 3.8",
45
+ "Programming Language :: Python :: 3.9",
46
+ "Programming Language :: Python :: 3.10",
47
+ "Programming Language :: Python :: 3 :: Only",
48
+ ],
49
+ keywords=["webrtc", "realtime", "audio", "video", "livekit"],
50
+ license="Apache-2.0",
51
+ packages=setuptools.find_namespace_packages(
52
+ include=["livekit.*"]),
53
+ python_requires=">=3.7.0",
54
+ install_requires=[
55
+ "anthropic >= 0.7.5",
56
+ ],
57
+ package_data={},
58
+ project_urls={
59
+ "Documentation": "https://docs.livekit.io",
60
+ "Website": "https://livekit.io/",
61
+ "Source": "https://github.com/livekit/python-agents",
62
+ },
63
+ )