Skip to content

Commit 3a6b88e

Browse files
marklyszeekzhugagbjackgerrits
authored
Ollama Client (with tool calling) (#3056)
* Ollama client! With function calling. Initial commit, client, no docs or tests yet. * Tidy comments * Cater for missing prompt token count * Removed use of eval, added json parsing support library * Fix to the use of the JSON fix library, handling of Mixtral escape sequence * Fixed 'name' in JSON bug, catered for single function call JSON without [] * removing role='tool' from inner tool result to reduce token usage. * Added Ollama documentation and updated library versions * Added Native Ollama tool calling (v0.3.0 req.) as well as hide/show tools support * Added native tool calling and hide_tools parameter to documentation * Update to Ollama 0.3.1, added tests * Tweak to manual function calling prompt to improve number handling. * Fix formatting Co-authored-by: gagb <[email protected]> Co-authored-by: Jack Gerrits <[email protected]> * Fix formatting * Better error message --------- Co-authored-by: Eric Zhu <[email protected]> Co-authored-by: gagb <[email protected]> Co-authored-by: Jack Gerrits <[email protected]>
1 parent db28718 commit 3a6b88e

File tree

9 files changed

+1563
-0
lines changed

9 files changed

+1563
-0
lines changed

.github/workflows/contrib-tests.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,35 @@ jobs:
709709
with:
710710
file: ./coverage.xml
711711
flags: unittests
712+
713+
OllamaTest:
714+
runs-on: ${{ matrix.os }}
715+
strategy:
716+
fail-fast: false
717+
matrix:
718+
os: [ubuntu-latest, macos-latest, windows-2019]
719+
python-version: ["3.9", "3.10", "3.11", "3.12"]
720+
exclude:
721+
- os: macos-latest
722+
python-version: "3.9"
723+
steps:
724+
- uses: actions/checkout@v4
725+
with:
726+
lfs: true
727+
- name: Set up Python ${{ matrix.python-version }}
728+
uses: actions/setup-python@v5
729+
with:
730+
python-version: ${{ matrix.python-version }}
731+
- name: Install packages and dependencies for all tests
732+
run: |
733+
python -m pip install --upgrade pip wheel
734+
pip install pytest-cov>=5
735+
- name: Install packages and dependencies for Ollama
736+
run: |
737+
pip install -e .[ollama,test]
738+
pytest test/oai/test_ollama.py --skip-openai
739+
- name: Upload coverage to Codecov
740+
uses: codecov/codecov-action@v3
741+
with:
742+
file: ./coverage.xml
743+
flags: unittests

autogen/logger/file_logger.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from autogen.oai.gemini import GeminiClient
2525
from autogen.oai.groq import GroqClient
2626
from autogen.oai.mistral import MistralAIClient
27+
from autogen.oai.ollama import OllamaClient
2728
from autogen.oai.together import TogetherClient
2829

2930
logger = logging.getLogger(__name__)
@@ -218,6 +219,7 @@ def log_new_client(
218219
| TogetherClient
219220
| GroqClient
220221
| CohereClient
222+
| OllamaClient
221223
| BedrockClient
222224
),
223225
wrapper: OpenAIWrapper,

autogen/logger/sqlite_logger.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from autogen.oai.gemini import GeminiClient
2626
from autogen.oai.groq import GroqClient
2727
from autogen.oai.mistral import MistralAIClient
28+
from autogen.oai.ollama import OllamaClient
2829
from autogen.oai.together import TogetherClient
2930

3031
logger = logging.getLogger(__name__)
@@ -405,6 +406,7 @@ def log_new_client(
405406
TogetherClient,
406407
GroqClient,
407408
CohereClient,
409+
OllamaClient,
408410
BedrockClient,
409411
],
410412
wrapper: OpenAIWrapper,

autogen/oai/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@
9393
except ImportError as e:
9494
cohere_import_exception = e
9595

96+
try:
97+
from autogen.oai.ollama import OllamaClient
98+
99+
ollama_import_exception: Optional[ImportError] = None
100+
except ImportError as e:
101+
ollama_import_exception = e
102+
96103
try:
97104
from autogen.oai.bedrock import BedrockClient
98105

@@ -545,6 +552,11 @@ def _register_default_client(self, config: Dict[str, Any], openai_config: Dict[s
545552
raise ImportError("Please install `cohere` to use the Cohere API.")
546553
client = CohereClient(**openai_config)
547554
self._clients.append(client)
555+
elif api_type is not None and api_type.startswith("ollama"):
556+
if ollama_import_exception:
557+
raise ImportError("Please install with `[ollama]` option to use the Ollama API.")
558+
client = OllamaClient(**openai_config)
559+
self._clients.append(client)
548560
elif api_type is not None and api_type.startswith("bedrock"):
549561
self._configure_openai_config_for_bedrock(config, openai_config)
550562
if bedrock_import_exception:

0 commit comments

Comments
 (0)