196 lines
7.1 KiB
Plaintext
196 lines
7.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "0bf0e410-0031-4983-a868-f2f1253f6947",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from typing import Optional\n",
|
|
"from pydantic_settings import BaseSettings\n",
|
|
"from functools import lru_cache\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"import os\n",
|
|
"\n",
|
|
"\n",
|
|
"class LLMProviderSettings(BaseSettings):\n",
|
|
" temperature: float = 0.0\n",
|
|
" max_tokens: Optional[int] = None\n",
|
|
" max_retries: int = 3\n",
|
|
" base_url: str = 'http://192.168.20.30:7869/v1' ## Added as all models being used in this case are served by Ollama\n",
|
|
"\n",
|
|
"class Llama3Settings(LLMProviderSettings):\n",
|
|
" api_key: str = \"key\" # required, but not used\n",
|
|
" default_model: str = \"llama3.2:latest\"\n",
|
|
"\n",
|
|
"class Phi3b8Settings(LLMProviderSettings):\n",
|
|
" api_key: str = \"key\" # required, but not used\n",
|
|
" default_model: str = \"phi3:3.8b\" \n",
|
|
" \n",
|
|
"class Phi14bSettings(LLMProviderSettings):\n",
|
|
" api_key: str = \"key\" # required, but not used\n",
|
|
" default_model: str = \"phi3:14b\"\n",
|
|
" \n",
|
|
"class DeepSeekSettings(LLMProviderSettings):\n",
|
|
" api_key: str = \"key\" # required, but not used\n",
|
|
" default_model: str = \"deepseek-coder-v2:latest\"\n",
|
|
" \n",
|
|
" \n",
|
|
"class GemmaSettings(LLMProviderSettings):\n",
|
|
" api_key: str = \"key\" # required, but not used\n",
|
|
" default_model: str = \"gemma2:27b\"\n",
|
|
" \n",
|
|
" \n",
|
|
"class Settings(BaseSettings):\n",
|
|
" app_name: str = \"GenAI Project Template\"\n",
|
|
" # openai: OpenAISettings = OpenAISettings()\n",
|
|
" # anthropic: AnthropicSettings = AnthropicSettings()\n",
|
|
" llama: Llama3Settings = Llama3Settings()\n",
|
|
" phi3b8: Phi3b8Settings = Phi3b8Settings()\n",
|
|
" phi14b: Phi14bSettings = Phi14bSettings()\n",
|
|
" deepseek: DeepSeekSettings = DeepSeekSettings()\n",
|
|
" gemma: GemmaSettings = GemmaSettings()\n",
|
|
"\n",
|
|
"@lru_cache\n",
|
|
"def get_settings():\n",
|
|
" return Settings()\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "f4993f4b-398e-4ad3-938f-5421677939c4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from typing import Any, Dict, List, Type\n",
|
|
"\n",
|
|
"import instructor\n",
|
|
"# from anthropic import Anthropic\n",
|
|
"# from config.settings import get_settings\n",
|
|
"from openai import OpenAI\n",
|
|
"from pydantic import BaseModel, Field\n",
|
|
"\n",
|
|
"class LLMFactory:\n",
|
|
" def __init__(self, provider: str):\n",
|
|
" self.provider = provider\n",
|
|
" self.settings = getattr(get_settings(), provider)\n",
|
|
" self.client = self._initialize_client()\n",
|
|
"\n",
|
|
" \n",
|
|
" def _initialize_client(self) -> Any:\n",
|
|
" client_initializers = {\n",
|
|
" \"llama\": lambda s: instructor.from_openai(\n",
|
|
" OpenAI(base_url=s.base_url, api_key=s.api_key),\n",
|
|
" mode=instructor.Mode.JSON,\n",
|
|
" ),\n",
|
|
" \"phi3b8\": lambda s: instructor.from_openai(\n",
|
|
" OpenAI(base_url=s.base_url, api_key=s.api_key),\n",
|
|
" mode=instructor.Mode.JSON,\n",
|
|
" ),\n",
|
|
" \"phi14b\": lambda s: instructor.from_openai(\n",
|
|
" OpenAI(base_url=s.base_url, api_key=s.api_key),\n",
|
|
" mode=instructor.Mode.JSON,\n",
|
|
" ),\n",
|
|
" \"deepseek\": lambda s: instructor.from_openai(\n",
|
|
" OpenAI(base_url=s.base_url, api_key=s.api_key),\n",
|
|
" mode=instructor.Mode.JSON,\n",
|
|
" ),\n",
|
|
" \"gemma\": lambda s: instructor.from_openai(\n",
|
|
" OpenAI(base_url=s.base_url, api_key=s.api_key),\n",
|
|
" mode=instructor.Mode.JSON,\n",
|
|
" ),\n",
|
|
" }\n",
|
|
"\n",
|
|
" initializer = client_initializers.get(self.provider)\n",
|
|
" if initializer:\n",
|
|
" return initializer(self.settings)\n",
|
|
" raise ValueError(f\"Unsupported LLM provider: {self.provider}\")\n",
|
|
"\n",
|
|
" def create_completion(\n",
|
|
" self, response_model: Type[BaseModel], messages: List[Dict[str, str]], **kwargs\n",
|
|
" ) -> Any:\n",
|
|
" completion_params = {\n",
|
|
" \"model\": kwargs.get(\"model\", self.settings.default_model),\n",
|
|
" \"temperature\": kwargs.get(\"temperature\", self.settings.temperature),\n",
|
|
" \"max_retries\": kwargs.get(\"max_retries\", self.settings.max_retries),\n",
|
|
" \"max_tokens\": kwargs.get(\"max_tokens\", self.settings.max_tokens),\n",
|
|
" \"response_model\": response_model,\n",
|
|
" \"messages\": messages,\n",
|
|
" }\n",
|
|
" return self.client.chat.completions.create(**completion_params)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "fa7ea636-7abe-4dc4-9665-1eb9bc4fde17",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Response: It will still take approximately 2 hours to dry 5 shirts.\n",
|
|
"\n",
|
|
"Reasoning: Drying time for clothes in the sun is primarily determined by factors like sunlight intensity, temperature, and humidity. Assuming these conditions remain constant, adding more shirts won't significantly increase the drying time. Each shirt will dry independently of the others.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"class CompletionModel(BaseModel):\n",
|
|
" response: str = Field(description=\"Your response to the user.\")\n",
|
|
" reasoning: str = Field(description=\"Explain your reasoning for the response.\")\n",
|
|
"\n",
|
|
"messages = [\n",
|
|
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
|
|
" {\n",
|
|
" \"role\": \"user\",\n",
|
|
" \"content\": \"If it takes 2 hours to dry 1 shirt out in the sun, how long will it take to dry 5 shirts?\",\n",
|
|
" },\n",
|
|
"]\n",
|
|
"\n",
|
|
"llm = LLMFactory(\"gemma\")\n",
|
|
"completion = llm.create_completion(\n",
|
|
" response_model=CompletionModel,\n",
|
|
" messages=messages,\n",
|
|
")\n",
|
|
"assert isinstance(completion, CompletionModel)\n",
|
|
"\n",
|
|
"print(f\"Response: {completion.response}\\n\")\n",
|
|
"print(f\"Reasoning: {completion.reasoning}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c42495cd-5e8e-48bc-aaea-e36f18b02baf",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.1"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|