forked from neo4j/neo4j-graphrag-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanthropic_llm.py
More file actions
133 lines (113 loc) · 4.88 KB
/
anthropic_llm.py
File metadata and controls
133 lines (113 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import Any, Iterable, Optional
from pydantic import ValidationError
from neo4j_graphrag.exceptions import LLMGenerationError
from neo4j_graphrag.llm.base import LLMInterface
from neo4j_graphrag.llm.types import LLMResponse, MessageList, UserMessage
try:
import anthropic
from anthropic.types.message_param import MessageParam
except ImportError:
anthropic = None
class AnthropicLLM(LLMInterface):
"""Interface for large language models on Anthropic
Args:
model_name (str, optional): Name of the LLM to use. Defaults to "gemini-1.5-flash-001".
model_params (Optional[dict], optional): Additional parameters passed to the model when text is sent to it. Defaults to None.
system_instruction: Optional[str], optional): Additional instructions for setting the behavior and context for the model in a conversation. Defaults to None.
**kwargs (Any): Arguments passed to the model when for the class is initialised. Defaults to None.
Raises:
LLMGenerationError: If there's an error generating the response from the model.
Example:
.. code-block:: python
from neo4j_graphrag.llm import AnthropicLLM
llm = AnthropicLLM(
model_name="claude-3-opus-20240229",
model_params={"max_tokens": 1000},
api_key="sk...", # can also be read from env vars
)
llm.invoke("Who is the mother of Paul Atreides?")
"""
def __init__(
self,
model_name: str,
model_params: Optional[dict[str, Any]] = None,
system_instruction: Optional[str] = None,
**kwargs: Any,
):
if anthropic is None:
raise ImportError(
"""Could not import Anthropic Python client.
Please install it with `pip install "neo4j-graphrag[anthropic]"`."""
)
super().__init__(model_name, model_params, system_instruction)
self.anthropic = anthropic
self.client = anthropic.Anthropic(**kwargs)
self.async_client = anthropic.AsyncAnthropic(**kwargs)
def get_messages(
self, input: str, chat_history: Optional[list[Any]] = None
) -> Iterable[MessageParam]:
messages = []
if chat_history:
try:
MessageList(messages=chat_history)
except ValidationError as e:
raise LLMGenerationError(e.errors()) from e
messages.extend(chat_history)
messages.append(UserMessage(content=input).model_dump())
return messages
def invoke(
self, input: str, chat_history: Optional[list[Any]] = None
) -> LLMResponse:
"""Sends text to the LLM and returns a response.
Args:
input (str): The text to send to the LLM.
chat_history (Optional[list]): A collection previous messages, with each message having a specific role assigned.
Returns:
LLMResponse: The response from the LLM.
"""
try:
messages = self.get_messages(input, chat_history)
response = self.client.messages.create(
model=self.model_name,
system=self.system_instruction,
messages=messages,
**self.model_params,
)
return LLMResponse(content=response.content)
except self.anthropic.APIError as e:
raise LLMGenerationError(e)
async def ainvoke(
self, input: str, chat_history: Optional[list[Any]] = None
) -> LLMResponse:
"""Asynchronously sends text to the LLM and returns a response.
Args:
input (str): The text to send to the LLM.
chat_history (Optional[list]): A collection previous messages, with each message having a specific role assigned.
Returns:
LLMResponse: The response from the LLM.
"""
try:
messages = self.get_messages(input, chat_history)
response = await self.async_client.messages.create(
model=self.model_name,
system=self.system_instruction,
messages=messages,
**self.model_params,
)
return LLMResponse(content=response.content)
except self.anthropic.APIError as e:
raise LLMGenerationError(e)