Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions docs/edge/en/learn/custom-llm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,26 @@ If your LLM doesn't support stop words natively:
def call(self, messages, tools=None, callbacks=None, available_functions=None):
response = self._make_api_call(messages, tools)
content = response["choices"][0]["message"]["content"]
# Manually truncate at stop words

# Manually truncate at the earliest stop word, whatever the list order
if self.stop:
for stop_word in self.stop:
if stop_word in content:
content = content.split(stop_word)[0]
break
positions = [content.find(stop_word) for stop_word in self.stop]
positions = [position for position in positions if position != -1]
if positions:
content = content[: min(positions)].strip()

return content

def supports_stop_words(self) -> bool:
return False # Tell CrewAI we handle stop words manually
```

The cut goes at the **earliest** match, not at the first entry in `self.stop`, so
reordering the list does not change the result: `self.stop = ["LATE", "EARLY"]` and
`self.stop = ["EARLY", "LATE"]` both turn `"hello EARLY middle LATE tail"` into
`"hello"`. `BaseLLM._apply_stop_words` behaves the same way and also trims the
whitespace left before the stop word, which is why the example calls `.strip()`.

## Function Calling

If your LLM supports function calling, implement the complete flow:
Expand Down
20 changes: 13 additions & 7 deletions docs/edge/ko/learn/custom-llm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,26 @@ def supports_stop_words(self) -> bool:
def call(self, messages, tools=None, callbacks=None, available_functions=None):
response = self._make_api_call(messages, tools)
content = response["choices"][0]["message"]["content"]
# Manually truncate at stop words

# Manually truncate at the earliest stop word, whatever the list order
if self.stop:
for stop_word in self.stop:
if stop_word in content:
content = content.split(stop_word)[0]
break
positions = [content.find(stop_word) for stop_word in self.stop]
positions = [position for position in positions if position != -1]
if positions:
content = content[: min(positions)].strip()

return content

def supports_stop_words(self) -> bool:
return False # Tell CrewAI we handle stop words manually
```

절단은 `self.stop`의 첫 항목이 아니라 **가장 먼저 등장하는** 일치 지점에서 이뤄지므로,
목록 순서를 바꿔도 결과가 달라지지 않습니다. `self.stop = ["LATE", "EARLY"]`와
`self.stop = ["EARLY", "LATE"]` 모두 `"hello EARLY middle LATE tail"`을 `"hello"`로 만듭니다.
`BaseLLM._apply_stop_words`도 같은 방식으로 동작하며 스톱 워드 앞의 공백까지 제거하므로,
예제에서 `.strip()`을 호출합니다.

## 함수 호출

LLM이 함수 호출을 지원하는 경우, 전체 플로우를 구현하세요:
Expand Down
59 changes: 59 additions & 0 deletions lib/crewai/tests/test_custom_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,62 @@ def test_timeout_handling_llm():
with pytest.raises(TimeoutError, match="LLM request failed after 2 attempts"):
llm.call("Test message")
assert len(llm.calls) == 2 # Initial call + failed retry attempt


class ManualStopWordLLM(BaseLLM):
"""The manual stop-word fallback documented in ``docs/edge/en/learn/custom-llm.mdx``.

Kept next to the guide's example so the documented snippet and its check
cannot drift apart: the truncation below is the one the guide shows.
"""

def __init__(self, response: str, stop: List[str]):
super().__init__(model="test-model")
self.response = response
self.stop = stop

def call(
self,
messages: Union[str, List[Dict[str, str]]],
tools: Optional[List[dict]] = None,
callbacks: Optional[List[Any]] = None,
available_functions: Optional[Dict[str, Any]] = None,
from_task=None,
from_agent=None,
response_model=None,
) -> str:
content = self.response

# Manually truncate at the earliest stop word, whatever the list order
if self.stop:
positions = [content.find(stop_word) for stop_word in self.stop]
positions = [position for position in positions if position != -1]
if positions:
content = content[: min(positions)].strip()

return content

def supports_function_calling(self) -> bool:
return False

def supports_stop_words(self) -> bool:
return False

def get_context_window_size(self) -> int:
return 8192

async def acall(self, messages, tools=None, callbacks=None, available_functions=None, from_task=None, from_agent=None, response_model=None):
raise NotImplementedError


@pytest.mark.parametrize("stop", [["LATE", "EARLY"], ["EARLY", "LATE"]])
def test_documented_stop_word_fallback_stops_at_the_earliest_match(stop: List[str]) -> None:
"""Both stop-list orders cut at EARLY, the earliest match in the response."""
llm = ManualStopWordLLM("hello EARLY middle LATE tail", stop)
assert llm.call("Test message") == "hello"


def test_documented_stop_word_fallback_leaves_unmatched_responses_alone() -> None:
"""A response with no stop word is returned unchanged."""
llm = ManualStopWordLLM("nothing to cut here", ["LATE", "EARLY"])
assert llm.call("Test message") == "nothing to cut here"