|
| 1 | +# ty: ignore[unresolved-import] |
| 2 | +from typing import Tuple |
| 3 | + |
| 4 | +import llama_index.core |
| 5 | +from langchain_openai import AzureOpenAIEmbeddings, OpenAIEmbeddings |
| 6 | +from llama_index.core.base.embeddings.base import BaseEmbedding |
| 7 | +from llama_index.core.llms import LLM |
| 8 | +from llama_index.embeddings.langchain import LangchainEmbedding |
| 9 | +from llama_index.embeddings.ollama import OllamaEmbedding |
| 10 | +from llama_index.llms.azure_openai import AzureOpenAI |
| 11 | +from llama_index.llms.ollama import Ollama |
| 12 | +from llama_index.llms.openai import OpenAI |
| 13 | + |
| 14 | +from cratedb_toolkit.query.llm.model import ModelInfo, ModelProvider |
| 15 | + |
| 16 | + |
| 17 | +def configure_llm(info: ModelInfo, debug: bool = False) -> Tuple[LLM, BaseEmbedding]: |
| 18 | + """ |
| 19 | + Configure LLM access and model types. Use either vanilla Open AI, Azure Open AI, or Ollama. |
| 20 | +
|
| 21 | + TODO: What about Hugging Face, Runpod, vLLM, and others? |
| 22 | +
|
| 23 | + Notes about text embedding models: |
| 24 | +
|
| 25 | + > The new model, `text-embedding-ada-002`, replaces five separate models for text search, |
| 26 | + > text similarity, and code search, and outperforms our previous most capable model, |
| 27 | + > Davinci, at most tasks, while being priced 99.8% lower. |
| 28 | +
|
| 29 | + - https://openai.com/index/new-and-improved-embedding-model/ |
| 30 | + - https://community.openai.com/t/models-embedding-vs-similarity-vs-search-models/291265 |
| 31 | + """ |
| 32 | + |
| 33 | + completion_model = info.completion |
| 34 | + |
| 35 | + if not info.provider: |
| 36 | + raise ValueError("LLM model type not defined") |
| 37 | + if not completion_model: |
| 38 | + raise ValueError("LLM model name not defined") |
| 39 | + |
| 40 | + # https://docs.llamaindex.ai/en/stable/understanding/tracing_and_debugging/tracing_and_debugging/ |
| 41 | + if debug: |
| 42 | + llama_index.core.set_global_handler("simple") |
| 43 | + |
| 44 | + # Select completions model. |
| 45 | + if info.provider is ModelProvider.OPENAI: |
| 46 | + llm = OpenAI( |
| 47 | + model=completion_model, |
| 48 | + temperature=0.0, |
| 49 | + api_key=info.api_key, |
| 50 | + api_version=info.api_version, |
| 51 | + ) |
| 52 | + elif info.provider is ModelProvider.AZURE: |
| 53 | + llm = AzureOpenAI( |
| 54 | + model=completion_model, |
| 55 | + temperature=0.0, |
| 56 | + engine=info.instance, |
| 57 | + azure_endpoint=info.endpoint, |
| 58 | + api_key=info.api_key, |
| 59 | + api_version=info.api_version, |
| 60 | + ) |
| 61 | + elif info.provider is ModelProvider.OLLAMA: |
| 62 | + # https://docs.llamaindex.ai/en/stable/api_reference/llms/ollama/ |
| 63 | + llm = Ollama( |
| 64 | + base_url=info.endpoint or "http://localhost:11434", |
| 65 | + model=completion_model, |
| 66 | + temperature=0.0, |
| 67 | + request_timeout=120.0, |
| 68 | + keep_alive=-1, |
| 69 | + ) |
| 70 | + else: |
| 71 | + raise ValueError("LLM model type invalid: %s", info.provider) |
| 72 | + |
| 73 | + # Select embeddings model. |
| 74 | + if info.provider is ModelProvider.OPENAI: |
| 75 | + embed_model = LangchainEmbedding( |
| 76 | + OpenAIEmbeddings( |
| 77 | + model=info.embedding, |
| 78 | + api_key=info.api_key, # ty: ignore[unknown-argument] |
| 79 | + ) |
| 80 | + ) |
| 81 | + elif info.provider is ModelProvider.AZURE: |
| 82 | + embed_model = LangchainEmbedding( |
| 83 | + AzureOpenAIEmbeddings( |
| 84 | + azure_endpoint=info.endpoint, |
| 85 | + model=info.embedding, |
| 86 | + api_key=info.api_key, # ty: ignore[unknown-argument] |
| 87 | + api_version=info.api_version, # ty: ignore[unknown-argument] |
| 88 | + ) |
| 89 | + ) |
| 90 | + # https://pypi.org/project/llama-index-embeddings-ollama/ |
| 91 | + # https://developers.llamaindex.ai/python/framework/integrations/embeddings/ollama_embedding/ |
| 92 | + # https://developers.llamaindex.ai/typescript/framework/modules/models/embeddings/ |
| 93 | + # Popular embedding models with Ollama: nomic-embed-text, embeddinggemma, mxbai-embed-large |
| 94 | + elif info.provider is ModelProvider.OLLAMA: |
| 95 | + embed_model = OllamaEmbedding( |
| 96 | + model_name=info.embedding, |
| 97 | + base_url=info.endpoint or "http://localhost:11434", |
| 98 | + ) |
| 99 | + else: |
| 100 | + embed_model = None |
| 101 | + |
| 102 | + return llm, embed_model |
0 commit comments