-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
146 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
import instructor | ||
from dotenv import load_dotenv | ||
from instructor.client import T | ||
from litellm import completion | ||
from llama_index.core.base.llms.types import ( | ||
CompletionResponse, | ||
CompletionResponseAsyncGen, | ||
) | ||
from llama_index.llms.litellm import LiteLLM | ||
|
||
load_dotenv() | ||
|
||
|
||
class BaseLLM(ABC): | ||
@abstractmethod | ||
async def astream(self, prompt: str) -> CompletionResponseAsyncGen: | ||
pass | ||
|
||
@abstractmethod | ||
def complete(self, prompt: str) -> CompletionResponse: | ||
pass | ||
|
||
@abstractmethod | ||
def structured_complete(self, response_model: type[T], prompt: str) -> T: | ||
pass | ||
|
||
|
||
class EveryLLM(BaseLLM): | ||
def __init__( | ||
self, | ||
model: str, | ||
): | ||
self.llm = LiteLLM(model=model) | ||
|
||
self.client = instructor.from_litellm(completion) | ||
|
||
async def astream(self, prompt: str) -> CompletionResponseAsyncGen: | ||
return await self.llm.astream_complete(prompt) | ||
|
||
def complete(self, prompt: str) -> CompletionResponse: | ||
return self.llm.complete(prompt) | ||
|
||
def structured_complete(self, response_model: type[T], prompt: str) -> T: | ||
return self.client.chat.completions.create( | ||
model=self.llm.model, | ||
messages=[{"role": "user", "content": prompt}], | ||
response_model=response_model, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,15 @@ | ||
import os | ||
|
||
import groq | ||
import instructor | ||
import openai | ||
from dotenv import load_dotenv | ||
|
||
from backend.constants import ChatModel, model_mappings | ||
from backend.llm.base import BaseLLM | ||
from backend.prompts import RELATED_QUESTION_PROMPT | ||
from backend.schemas import RelatedQueries, SearchResult | ||
|
||
load_dotenv() | ||
|
||
|
||
OLLAMA_HOST = os.environ.get("OLLAMA_HOST", "http://localhost:11434") | ||
|
||
|
||
def get_openai_client() -> openai.AsyncOpenAI: | ||
openai_mode = os.environ.get("OPENAI_MODE", "openai") | ||
if openai_mode == "openai": | ||
return openai.AsyncOpenAI() | ||
elif openai_mode == "azure": | ||
return openai.AsyncAzureOpenAI( | ||
azure_deployment=os.environ.get("AZURE_DEPLOYMENT_NAME"), | ||
azure_endpoint=os.environ["AZURE_CHAT_ENDPOINT"], | ||
api_key=os.environ.get("AZURE_API_KEY"), | ||
api_version="2024-04-01-preview", | ||
) | ||
else: | ||
raise ValueError(f"Unknown openai mode: {openai_mode}") | ||
|
||
|
||
def instructor_client(model: ChatModel) -> instructor.AsyncInstructor: | ||
if model == ChatModel.GPT_3_5_TURBO: | ||
return instructor.from_openai( | ||
get_openai_client(), | ||
) | ||
elif model in [ | ||
ChatModel.GPT_3_5_TURBO, | ||
ChatModel.GPT_4o, | ||
]: | ||
return instructor.from_openai(openai.AsyncOpenAI()) | ||
elif model in [ | ||
ChatModel.LOCAL_GEMMA, | ||
ChatModel.LOCAL_LLAMA_3, | ||
ChatModel.LOCAL_MISTRAL, | ||
ChatModel.LOCAL_PHI3_14B, | ||
]: | ||
return instructor.from_openai( | ||
openai.AsyncOpenAI( | ||
base_url=f"{OLLAMA_HOST}/v1", | ||
api_key="ollama", | ||
), | ||
mode=instructor.Mode.JSON, | ||
) | ||
elif model == ChatModel.LLAMA_3_70B: | ||
return instructor.from_groq(groq.AsyncGroq(), mode=instructor.Mode.JSON) # type: ignore | ||
else: | ||
raise ValueError(f"Unknown model: {model}") | ||
|
||
|
||
async def generate_related_queries( | ||
query: str, search_results: list[SearchResult], model: ChatModel | ||
query: str, search_results: list[SearchResult], llm: BaseLLM | ||
) -> list[str]: | ||
context = "\n\n".join([f"{str(result)}" for result in search_results]) | ||
# Truncate the context to 4000 characters (mainly for smaller models) | ||
context = context[:4000] | ||
|
||
client = instructor_client(model) | ||
model_name = model_mappings[model] | ||
|
||
print(RELATED_QUESTION_PROMPT.format(query=query, context=context)) | ||
|
||
related = await client.chat.completions.create( | ||
model=model_name, | ||
response_model=RelatedQueries, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": RELATED_QUESTION_PROMPT.format(query=query, context=context), | ||
}, | ||
], | ||
related = llm.structured_complete( | ||
RelatedQueries, RELATED_QUESTION_PROMPT.format(query=query, context=context) | ||
) | ||
|
||
return [query.lower().replace("?", "") for query in related.related_questions] |