Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doomgrave patch 1 #696

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</picture></div>
<br/>

ORIGINAL GIT: https://github.com/guidance-ai/guidance

> *Note that v0.1 is a dramatically new version developed while releases had to be paused over the summer. If you are looking for the old version based on handlebars, you can use v0.0.64, but you should instead try porting over to the much better new version :)*

Expand Down
2 changes: 2 additions & 0 deletions guidance/langchain/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .embeddings import LlamaCppEmbeddings_Guidance
from .llamacpp import LlamaCpp_LLM
74 changes: 74 additions & 0 deletions guidance/langchain/embeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from typing import Any, Dict, List, Optional
from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, Extra, Field, root_validator


class LlamaCppEmbeddings_Guidance(BaseModel, Embeddings):
"""llama.cpp embedding models using Guidance

To use, you should have the llama-cpp-python and langchain library installed.
LlamaCpp istance must have embedding = true.

USAGE EXAMPLE (using Chroma database):
llama2 = guidance.models.LlamaCpp(model=modelPath,n_gpu_layers=-1,n_ctx=4096,embedding = true)
embeddings = GuidanceLlamaCppEmbeddings(client=llama2)
vectordb = Chroma(persist_directory={path_to_chromadb}, embedding_function=embeddings)
"""
model: Any
client: Optional[Any]

class Config:
"""Configuration for this pydantic object."""

extra = Extra.forbid

@root_validator()
def validate_environment(cls, values: Dict) -> Dict:

"""Validate that llama-cpp-python library is installed."""
try:
if values["model"].engine.model_obj:
values["client"] = values["model"].engine.model_obj
return values

if values["model"].model_obj:
values["client"] = values["model"].model_obj
return values

raise ModuleNotFoundError("Could not import llama-cpp-python library or incompatible version.")
except ImportError:
raise ModuleNotFoundError(
"Could not import llama-cpp-python library. "
"Please install the llama-cpp-python library to "
"use this embedding model: pip install llama-cpp-python"
)
except Exception as e:
raise ValueError(
f"Received error {e}"
)
return values


def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Embed a list of documents using the Llama model.

Args:
texts: The list of texts to embed.

Returns:
List of embeddings, one for each text.
"""
embeddings = [self.client.embed(text) for text in texts]
return [list(map(float, e)) for e in embeddings]

def embed_query(self, text: str) -> List[float]:
"""Embed a query using the Llama model.

Args:
text: The text to embed.

Returns:
Embeddings for the text.
"""
embedding = self.client.embed(text)
return list(map(float, embedding))
Loading