Skip to content

Commit 90c7872

Browse files
committed
Add basic langchain demo
1 parent eec9256 commit 90c7872

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

examples/langchain_custom_llm.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from llama_cpp import Llama
2+
3+
from langchain.llms.base import LLM
4+
from typing import Optional, List, Mapping, Any
5+
6+
class LlamaLLM(LLM):
7+
model_path: str
8+
llm: Llama
9+
10+
@property
11+
def _llm_type(self) -> str:
12+
return "llama-cpp-python"
13+
14+
def __init__(self, model_path: str, **kwargs: Any):
15+
model_path = model_path
16+
llm = Llama(model_path=model_path)
17+
super().__init__(model_path=model_path, llm=llm, **kwargs)
18+
19+
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
20+
response = self.llm(prompt, stop=stop or [])
21+
return response["choices"][0]["text"]
22+
23+
@property
24+
def _identifying_params(self) -> Mapping[str, Any]:
25+
return {"model_path": self.model_path}
26+
27+
llm = LlamaLLM(model_path="models/...")
28+
29+
print(llm("Question: What is the capital of France? Answer: ", stop=["Question:", "\n"]))

0 commit comments

Comments
 (0)