""" Примеры использования Ollama OpenAI-Compatible провайдера ========================================================= Запуск Ollama перед стартом: ollama serve ollama pull llama3.2 ollama pull nomic-embed-text # для эмбеддингов ollama pull llava # для vision (опционально) """ import json from hf_ollama_adapter import create_client, OllamaProvider from ollama_openai_provider import OllamaProvider # =========================================================================== # 1. ПРОСТОЙ ЧАТ # =========================================================================== def example_simple_chat(): print("=" * 60) print("1. Простой чат") print("=" * 60) client = OllamaProvider() response = client.chat.completions.create( model="llama3.2", messages=[ {"role": "system", "content": "Ты полезный ассистент. Отвечай кратко."}, {"role": "user", "content": "Что такое Hugging Face?"}, ], temperature=0.7, max_tokens=200, ) print(f"Ответ: {response.choices[0].message.content}") if response.usage: print(f"Токены: {response.usage.total_tokens}") print() # =========================================================================== # 2. СТРИМИНГ # =========================================================================== def example_streaming(): print("=" * 60) print("2. Стриминг (stream=True)") print("=" * 60) client = OllamaProvider() print("Ответ: ", end="") for chunk in client.chat.completions.create( model="llama3.2", messages=[{"role": "user", "content": "Напиши короткое стихотворение о Python."}], stream=True, ): delta = chunk.choices[0].delta if delta.content: print(delta.content, end="", flush=True) print("\n") # =========================================================================== # 3. TOOL CALLING (FUNCTION CALLING) # =========================================================================== def example_tool_calling(): print("=" * 60) print("3. Tool Calling") print("=" * 60) client = OllamaProvider() # Определяем инструменты tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Получить текущую погоду в указанном городе", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "Город и страна, например 'Варшава, Польша'", }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Единица температуры", }, }, "required": ["location"], }, }, }, { "type": "function", "function": { "name": "search_web", "description": "Поиск актуальной информации в интернете", "parameters": { "type": "object", "properties": { "query": { "type": "string", "description": "Поисковый запрос", } }, "required": ["query"], }, }, }, ] messages = [ {"role": "user", "content": "Какая сейчас погода в Варшаве?"} ] # --- Шаг 1: модель решает вызвать инструмент --- response = client.chat.completions.create( model="llama3.2", messages=messages, tools=tools, tool_choice="auto", ) msg = response.choices[0].message print(f"Finish reason: {response.choices[0].finish_reason}") if msg.tool_calls: print("Модель хочет вызвать инструменты:") for tc in msg.tool_calls: fn = tc.function args = json.loads(fn.arguments) print(f" → {fn.name}({args})") # --- Шаг 2: добавляем ответ ассистента в историю --- messages.append(msg.to_dict()) # --- Шаг 3: имитируем выполнение инструмента --- for tc in msg.tool_calls: tool_result = ( '{"temperature": 12, "condition": "Облачно", "humidity": 78}' if tc.function.name == "get_weather" else '{"results": ["Результат 1", "Результат 2"]}' ) messages.append({ "role": "tool", "tool_call_id": tc.id, "content": tool_result, }) # --- Шаг 4: финальный ответ с данными инструмента --- final_response = client.chat.completions.create( model="llama3.2", messages=messages, tools=tools, ) print(f"\nФинальный ответ: {final_response.choices[0].message.content}") else: print(f"Прямой ответ: {msg.content}") print() # =========================================================================== # 4. MULTI-TURN ДИАЛОГ # =========================================================================== def example_multi_turn(): print("=" * 60) print("4. Multi-turn диалог") print("=" * 60) client = OllamaProvider() history = [ {"role": "system", "content": "Ты опытный Python разработчик. Давай краткие ответы."} ] def chat(user_msg: str) -> str: history.append({"role": "user", "content": user_msg}) response = client.chat.completions.create( model="llama3.2", messages=history, temperature=0.3, ) reply = response.choices[0].message.content history.append({"role": "assistant", "content": reply}) return reply turns = [ "Что такое декоратор в Python?", "Покажи простой пример.", "Как добавить аргументы к декоратору?", ] for user_input in turns: print(f"Пользователь: {user_input}") reply = chat(user_input) print(f"Ассистент: {reply[:200]}{'...' if len(reply) > 200 else ''}") print() # =========================================================================== # 5. STRUCTURED OUTPUT (JSON mode) # =========================================================================== def example_structured_output(): print("=" * 60) print("5. Structured output (JSON)") print("=" * 60) client = OllamaProvider() response = client.chat.completions.create( model="llama3.2", messages=[ { "role": "system", "content": "Отвечай ТОЛЬКО валидным JSON без пояснений.", }, { "role": "user", "content": ( "Верни информацию о 3 крупнейших языках программирования " "в виде JSON массива с полями: name, year_created, paradigm, popularity_rank" ), }, ], temperature=0.1, ) raw = response.choices[0].message.content try: # Убираем возможные markdown-блоки clean = raw.strip().removeprefix("```json").removeprefix("```").removesuffix("```").strip() data = json.loads(clean) print(f"Получено записей: {len(data)}") for item in data: print(f" {item.get('popularity_rank', '?')}. {item.get('name')} ({item.get('year_created')})") except json.JSONDecodeError: print(f"Сырой ответ: {raw}") print() # =========================================================================== # 6. EMBEDDINGS # =========================================================================== def example_embeddings(): print("=" * 60) print("6. Embeddings") print("=" * 60) client = OllamaProvider() texts = [ "Машинное обучение — это подраздел искусственного интеллекта", "Python — популярный язык программирования", "Нейронные сети вдохновлены мозгом человека", ] response = client.embeddings.create( model="nomic-embed-text", # или "mxbai-embed-large" input=texts, ) for emb in response.data: vec = emb.embedding print(f" Текст {emb.index}: вектор размером {len(vec)}, первые 5 = {vec[:5]}") print() # =========================================================================== # 7. VISION (мультимодальность) # =========================================================================== def example_vision(): print("=" * 60) print("7. Vision (мультимодальность)") print("=" * 60) import base64, urllib.request # Загружаем тестовое изображение url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png" with urllib.request.urlopen(url) as resp: img_data = base64.b64encode(resp.read()).decode() client = OllamaProvider() response = client.chat.completions.create( model="llava", # нужна мультимодальная модель messages=[ { "role": "user", "content": [ { "type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_data}"}, }, {"type": "text", "text": "Опиши это изображение одним предложением."}, ], } ], ) print(f"Описание: {response.choices[0].message.content}") print() # =========================================================================== # 8. LIST MODELS # =========================================================================== def example_list_models(): print("=" * 60) print("8. Список доступных моделей") print("=" * 60) client = OllamaProvider() models = client.models.list() print(f"Найдено моделей: {len(models.data)}") for m in models.data: print(f" - {m.id}") print() # =========================================================================== # ЗАПУСК ВСЕХ ПРИМЕРОВ # =========================================================================== if __name__ == "__main__": import sys examples = { "chat": example_simple_chat, "stream": example_streaming, "tools": example_tool_calling, "multi": example_multi_turn, "json": example_structured_output, "embed": example_embeddings, "vision": example_vision, "models": example_list_models, } if len(sys.argv) > 1: name = sys.argv[1] if name in examples: examples[name]() else: print(f"Неизвестный пример: {name}") print(f"Доступные: {', '.join(examples)}") else: # Запускаем примеры без тяжёлых зависимостей for name in ["chat", "stream", "tools", "multi", "json", "models"]: try: examples[name]() except Exception as e: print(f"[{name}] Ошибка: {e}\n")