LLMatie/LLM_Factory.ipynb

255 lines
9.8 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": 10,
"id": "c42495cd-5e8e-48bc-aaea-e36f18b02baf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
" llama\n",
"Response: It will take 10 hours to dry 5 shirts\n",
"\n",
"Reasoning: Since it takes 2 hours to dry 1 shirt, you can multiply that time by 5 to get the total drying time for 5 shirts.\n",
"\n",
"\n",
" phi3b8\n",
"Response: It would still take approximately 2 hours to dry all five shirts if they are dried simultaneously under optimal conditions.\n",
"\n",
"Reasoning: Assuming that each of the 5 shirts can be hung up and exposed to sunlight at once without overlapping, it is reasonable to expect them to dry in about the same time as one would take for a single shirt. However, this assumes ideal weather conditions with no cloud cover or rain.\n",
"\n",
"\n",
" phi14b\n",
"Response: It would still take 2 hours to dry all 5 shirts if they are dried simultaneously. However, if you can only dry one at a time, then it would take 10 hours.\n",
"\n",
"Reasoning: The drying time for each shirt is independent of the number of shirts being dried as long as there's enough space to lay them out in the sun without overlapping. If you have sufficient space and can hang all five at once, they will all be dry within 2 hours.\n",
"\n",
"\n",
" deepseek\n",
"Response: It will take 10 hours to dry 5 shirts.\n",
"\n",
"Reasoning: Since it takes 2 hours to dry 1 shirt, for 5 shirts, you would multiply the number of shirts by the time it takes to dry one shirt. So, 5 shirts * 2 hours/shirt = 10 hours.\n",
"\n",
"\n",
" gemma\n",
"Response: It will still take approximately 2 hours to dry 5 shirts.\n",
"\n",
"Reasoning: Drying time depends on factors like sunlight intensity, temperature, and humidity. Assuming these factors remain constant, each shirt dries independently of the others. So, 5 shirts would dry in roughly the same amount of time as one shirt.\n"
]
}
],
"source": [
"models = ['llama','phi3b8','phi14b','deepseek','gemma']\n",
"\n",
"for model in models:\n",
" print('\\n\\n',model)\n",
" llm = LLMFactory(model)\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": "53fc3412-506e-4c4e-8670-320e2980b3f5",
"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
}