Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
5c258e21
Unverified
Commit
5c258e21
authored
Mar 04, 2024
by
takatost
Committed by
GitHub
Mar 04, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add Anthropic claude-3 models support (#2684)
parent
6a6133c1
Changes
12
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
423 additions
and
148 deletions
+423
-148
anthropic.py
...core/model_runtime/model_providers/anthropic/anthropic.py
+1
-1
anthropic.yaml
...re/model_runtime/model_providers/anthropic/anthropic.yaml
+2
-2
_position.yaml
...odel_runtime/model_providers/anthropic/llm/_position.yaml
+6
-0
claude-2.yaml
...model_runtime/model_providers/anthropic/llm/claude-2.yaml
+1
-0
claude-3-opus-20240229.yaml
...model_providers/anthropic/llm/claude-3-opus-20240229.yaml
+37
-0
claude-3-sonnet-20240229.yaml
...del_providers/anthropic/llm/claude-3-sonnet-20240229.yaml
+37
-0
claude-instant-1.2.yaml
...ime/model_providers/anthropic/llm/claude-instant-1.2.yaml
+35
-0
claude-instant-1.yaml
...ntime/model_providers/anthropic/llm/claude-instant-1.yaml
+1
-0
llm.py
api/core/model_runtime/model_providers/anthropic/llm/llm.py
+227
-105
requirements.txt
api/requirements.txt
+1
-1
anthropic.py
...tests/integration_tests/model_runtime/__mock/anthropic.py
+68
-32
test_llm.py
...sts/integration_tests/model_runtime/anthropic/test_llm.py
+7
-7
No files found.
api/core/model_runtime/model_providers/anthropic/anthropic.py
View file @
5c258e21
...
...
@@ -21,7 +21,7 @@ class AnthropicProvider(ModelProvider):
# Use `claude-instant-1` model for validate,
model_instance
.
validate_credentials
(
model
=
'claude-instant-1'
,
model
=
'claude-instant-1
.2
'
,
credentials
=
credentials
)
except
CredentialsValidateFailedError
as
ex
:
...
...
api/core/model_runtime/model_providers/anthropic/anthropic.yaml
View file @
5c258e21
...
...
@@ -2,8 +2,8 @@ provider: anthropic
label
:
en_US
:
Anthropic
description
:
en_US
:
Anthropic’s powerful models, such as Claude
2 and Claude Instant
.
zh_Hans
:
Anthropic 的强大模型,例如 Claude
2 和 Claude Instant
。
en_US
:
Anthropic’s powerful models, such as Claude
3
.
zh_Hans
:
Anthropic 的强大模型,例如 Claude
3
。
icon_small
:
en_US
:
icon_s_en.svg
icon_large
:
...
...
api/core/model_runtime/model_providers/anthropic/llm/_position.yaml
0 → 100644
View file @
5c258e21
-
claude-3-opus-20240229
-
claude-3-sonnet-20240229
-
claude-2.1
-
claude-instant-1.2
-
claude-2
-
claude-instant-1
api/core/model_runtime/model_providers/anthropic/llm/claude-2.yaml
View file @
5c258e21
...
...
@@ -34,3 +34,4 @@ pricing:
output
:
'
24.00'
unit
:
'
0.000001'
currency
:
USD
deprecated
:
true
api/core/model_runtime/model_providers/anthropic/llm/claude-3-opus-20240229.yaml
0 → 100644
View file @
5c258e21
model
:
claude-3-opus-20240229
label
:
en_US
:
claude-3-opus-20240229
model_type
:
llm
features
:
-
agent-thought
-
vision
model_properties
:
mode
:
chat
context_size
:
200000
parameter_rules
:
-
name
:
temperature
use_template
:
temperature
-
name
:
top_p
use_template
:
top_p
-
name
:
top_k
label
:
zh_Hans
:
取样数量
en_US
:
Top k
type
:
int
help
:
zh_Hans
:
仅从每个后续标记的前 K 个选项中采样。
en_US
:
Only sample from the top K options for each subsequent token.
required
:
false
-
name
:
max_tokens
use_template
:
max_tokens
required
:
true
default
:
4096
min
:
1
max
:
4096
-
name
:
response_format
use_template
:
response_format
pricing
:
input
:
'
15.00'
output
:
'
75.00'
unit
:
'
0.000001'
currency
:
USD
api/core/model_runtime/model_providers/anthropic/llm/claude-3-sonnet-20240229.yaml
0 → 100644
View file @
5c258e21
model
:
claude-3-sonnet-20240229
label
:
en_US
:
claude-3-sonnet-20240229
model_type
:
llm
features
:
-
agent-thought
-
vision
model_properties
:
mode
:
chat
context_size
:
200000
parameter_rules
:
-
name
:
temperature
use_template
:
temperature
-
name
:
top_p
use_template
:
top_p
-
name
:
top_k
label
:
zh_Hans
:
取样数量
en_US
:
Top k
type
:
int
help
:
zh_Hans
:
仅从每个后续标记的前 K 个选项中采样。
en_US
:
Only sample from the top K options for each subsequent token.
required
:
false
-
name
:
max_tokens
use_template
:
max_tokens
required
:
true
default
:
4096
min
:
1
max
:
4096
-
name
:
response_format
use_template
:
response_format
pricing
:
input
:
'
3.00'
output
:
'
15.00'
unit
:
'
0.000001'
currency
:
USD
api/core/model_runtime/model_providers/anthropic/llm/claude-instant-1.2.yaml
0 → 100644
View file @
5c258e21
model
:
claude-instant-1.2
label
:
en_US
:
claude-instant-1.2
model_type
:
llm
features
:
[
]
model_properties
:
mode
:
chat
context_size
:
100000
parameter_rules
:
-
name
:
temperature
use_template
:
temperature
-
name
:
top_p
use_template
:
top_p
-
name
:
top_k
label
:
zh_Hans
:
取样数量
en_US
:
Top k
type
:
int
help
:
zh_Hans
:
仅从每个后续标记的前 K 个选项中采样。
en_US
:
Only sample from the top K options for each subsequent token.
required
:
false
-
name
:
max_tokens
use_template
:
max_tokens
required
:
true
default
:
4096
min
:
1
max
:
4096
-
name
:
response_format
use_template
:
response_format
pricing
:
input
:
'
1.63'
output
:
'
5.51'
unit
:
'
0.000001'
currency
:
USD
api/core/model_runtime/model_providers/anthropic/llm/claude-instant-1.yaml
View file @
5c258e21
...
...
@@ -33,3 +33,4 @@ pricing:
output
:
'
5.51'
unit
:
'
0.000001'
currency
:
USD
deprecated
:
true
api/core/model_runtime/model_providers/anthropic/llm/llm.py
View file @
5c258e21
This diff is collapsed.
Click to expand it.
api/requirements.txt
View file @
5c258e21
...
...
@@ -35,7 +35,7 @@ docx2txt==0.8
pypdfium2==4.16.0
resend~=0.7.0
pyjwt~=2.8.0
anthropic~=0.
7.7
anthropic~=0.
17.0
newspaper3k==0.2.8
google-api-python-client==2.90.0
wikipedia==1.4.0
...
...
api/tests/integration_tests/model_runtime/__mock/anthropic.py
View file @
5c258e21
import
os
from
time
import
sleep
from
typing
import
Any
,
Generator
,
List
,
Literal
,
Union
from
typing
import
Any
,
Literal
,
Union
,
Iterable
from
anthropic.resources
import
Messages
from
anthropic.types.message_delta_event
import
Delta
import
anthropic
import
pytest
from
_pytest.monkeypatch
import
MonkeyPatch
from
anthropic
import
Anthropic
from
anthropic.
_types
import
NOT_GIVEN
,
Body
,
Headers
,
NotGiven
,
Query
from
anthropic.resources.completions
import
Completions
from
anthropic.types
import
Completion
,
completion_create_params
from
anthropic
import
Anthropic
,
Stream
from
anthropic.
types
import
MessageParam
,
Message
,
MessageStreamEvent
,
\
ContentBlock
,
MessageStartEvent
,
Usage
,
TextDelta
,
MessageDeltaEvent
,
MessageStopEvent
,
ContentBlockDeltaEvent
,
\
MessageDeltaUsage
MOCK
=
os
.
getenv
(
'MOCK_SWITCH'
,
'false'
)
==
'true'
class
MockAnthropicClass
(
object
):
@
staticmethod
def
mocked_anthropic_chat_create_sync
(
model
:
str
)
->
Completion
:
return
Completion
(
completion
=
'hello, I
\'
m a chatbot from anthropic'
,
def
mocked_anthropic_chat_create_sync
(
model
:
str
)
->
Message
:
return
Message
(
id
=
'msg-123'
,
type
=
'message'
,
role
=
'assistant'
,
content
=
[
ContentBlock
(
text
=
'hello, I
\'
m a chatbot from anthropic'
,
type
=
'text'
)],
model
=
model
,
stop_reason
=
'stop_sequence'
stop_reason
=
'stop_sequence'
,
usage
=
Usage
(
input_tokens
=
1
,
output_tokens
=
1
)
)
@
staticmethod
def
mocked_anthropic_chat_create_stream
(
model
:
str
)
->
Generator
[
Completion
,
None
,
None
]:
def
mocked_anthropic_chat_create_stream
(
model
:
str
)
->
Stream
[
MessageStreamEvent
]:
full_response_text
=
"hello, I'm a chatbot from anthropic"
for
i
in
range
(
0
,
len
(
full_response_text
)
+
1
):
sleep
(
0.1
)
if
i
==
len
(
full_response_text
):
yield
Completion
(
completion
=
''
,
yield
MessageStartEvent
(
type
=
'message_start'
,
message
=
Message
(
id
=
'msg-123'
,
content
=
[],
role
=
'assistant'
,
model
=
model
,
stop_reason
=
None
,
type
=
'message'
,
usage
=
Usage
(
input_tokens
=
1
,
output_tokens
=
1
)
)
)
index
=
0
for
i
in
range
(
0
,
len
(
full_response_text
)):
sleep
(
0.1
)
yield
ContentBlockDeltaEvent
(
type
=
'content_block_delta'
,
delta
=
TextDelta
(
text
=
full_response_text
[
i
],
type
=
'text_delta'
),
index
=
index
)
index
+=
1
yield
MessageDeltaEvent
(
type
=
'message_delta'
,
delta
=
Delta
(
stop_reason
=
'stop_sequence'
),
usage
=
MessageDeltaUsage
(
output_tokens
=
1
)
else
:
yield
Completion
(
completion
=
full_response_text
[
i
],
model
=
model
,
stop_reason
=
''
)
def
mocked_anthropic
(
self
:
Completions
,
*
,
max_tokens_to_sample
:
int
,
model
:
Union
[
str
,
Literal
[
"claude-2.1"
,
"claude-instant-1"
]],
prompt
:
str
,
yield
MessageStopEvent
(
type
=
'message_stop'
)
def
mocked_anthropic
(
self
:
Messages
,
*
,
max_tokens
:
int
,
messages
:
Iterable
[
MessageParam
],
model
:
str
,
stream
:
Literal
[
True
],
**
kwargs
:
Any
)
->
Union
[
Completion
,
Generator
[
Completion
,
None
,
None
]]:
)
->
Union
[
Message
,
Stream
[
MessageStreamEvent
]]:
if
len
(
self
.
_client
.
api_key
)
<
18
:
raise
anthropic
.
AuthenticationError
(
'Invalid API key'
)
...
...
@@ -55,10 +90,11 @@ class MockAnthropicClass(object):
else
:
return
MockAnthropicClass
.
mocked_anthropic_chat_create_sync
(
model
=
model
)
@
pytest
.
fixture
def
setup_anthropic_mock
(
request
,
monkeypatch
:
MonkeyPatch
):
if
MOCK
:
monkeypatch
.
setattr
(
Completion
s
,
'create'
,
MockAnthropicClass
.
mocked_anthropic
)
monkeypatch
.
setattr
(
Message
s
,
'create'
,
MockAnthropicClass
.
mocked_anthropic
)
yield
...
...
api/tests/integration_tests/model_runtime/anthropic/test_llm.py
View file @
5c258e21
...
...
@@ -15,14 +15,14 @@ def test_validate_credentials(setup_anthropic_mock):
with
pytest
.
raises
(
CredentialsValidateFailedError
):
model
.
validate_credentials
(
model
=
'claude-instant-1'
,
model
=
'claude-instant-1
.2
'
,
credentials
=
{
'anthropic_api_key'
:
'invalid_key'
}
)
model
.
validate_credentials
(
model
=
'claude-instant-1'
,
model
=
'claude-instant-1
.2
'
,
credentials
=
{
'anthropic_api_key'
:
os
.
environ
.
get
(
'ANTHROPIC_API_KEY'
)
}
...
...
@@ -33,7 +33,7 @@ def test_invoke_model(setup_anthropic_mock):
model
=
AnthropicLargeLanguageModel
()
response
=
model
.
invoke
(
model
=
'claude-instant-1'
,
model
=
'claude-instant-1
.2
'
,
credentials
=
{
'anthropic_api_key'
:
os
.
environ
.
get
(
'ANTHROPIC_API_KEY'
),
'anthropic_api_url'
:
os
.
environ
.
get
(
'ANTHROPIC_API_URL'
)
...
...
@@ -49,7 +49,7 @@ def test_invoke_model(setup_anthropic_mock):
model_parameters
=
{
'temperature'
:
0.0
,
'top_p'
:
1.0
,
'max_tokens
_to_sample
'
:
10
'max_tokens'
:
10
},
stop
=
[
'How'
],
stream
=
False
,
...
...
@@ -64,7 +64,7 @@ def test_invoke_stream_model(setup_anthropic_mock):
model
=
AnthropicLargeLanguageModel
()
response
=
model
.
invoke
(
model
=
'claude-instant-1'
,
model
=
'claude-instant-1
.2
'
,
credentials
=
{
'anthropic_api_key'
:
os
.
environ
.
get
(
'ANTHROPIC_API_KEY'
)
},
...
...
@@ -78,7 +78,7 @@ def test_invoke_stream_model(setup_anthropic_mock):
],
model_parameters
=
{
'temperature'
:
0.0
,
'max_tokens
_to_sample
'
:
100
'max_tokens'
:
100
},
stream
=
True
,
user
=
"abc-123"
...
...
@@ -97,7 +97,7 @@ def test_get_num_tokens():
model
=
AnthropicLargeLanguageModel
()
num_tokens
=
model
.
get_num_tokens
(
model
=
'claude-instant-1'
,
model
=
'claude-instant-1
.2
'
,
credentials
=
{
'anthropic_api_key'
:
os
.
environ
.
get
(
'ANTHROPIC_API_KEY'
)
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment