mirror of
https://github.com/abetlen/llama-cpp-python.git
synced 2023-09-07 17:34:22 +03:00
Black formatting
This commit is contained in:
@@ -5,9 +5,11 @@ from llama_cpp import Llama
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, BaseSettings, Field
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model: str
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="🦙 llama.cpp Python API",
|
||||
version="0.0.1",
|
||||
@@ -15,6 +17,7 @@ app = FastAPI(
|
||||
settings = Settings()
|
||||
llama = Llama(settings.model)
|
||||
|
||||
|
||||
class CompletionRequest(BaseModel):
|
||||
prompt: str
|
||||
suffix: Optional[str] = Field(None)
|
||||
@@ -31,12 +34,11 @@ class CompletionRequest(BaseModel):
|
||||
schema_extra = {
|
||||
"example": {
|
||||
"prompt": "\n\n### Instructions:\nWhat is the capital of France?\n\n### Response:\n",
|
||||
"stop": ["\n", "###"]
|
||||
"stop": ["\n", "###"],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@app.post("/v1/completions")
|
||||
def completions(request: CompletionRequest):
|
||||
return llama(**request.dict())
|
||||
return llama(**request.dict())
|
||||
|
||||
@@ -9,6 +9,11 @@ args = parser.parse_args()
|
||||
|
||||
llm = Llama(model_path=args.model)
|
||||
|
||||
output = llm("Question: What are the names of the planets in the solar system? Answer: ", max_tokens=48, stop=["Q:", "\n"], echo=True)
|
||||
output = llm(
|
||||
"Question: What are the names of the planets in the solar system? Answer: ",
|
||||
max_tokens=48,
|
||||
stop=["Q:", "\n"],
|
||||
echo=True,
|
||||
)
|
||||
|
||||
print(json.dumps(output, indent=2))
|
||||
print(json.dumps(output, indent=2))
|
||||
|
||||
@@ -5,6 +5,7 @@ from llama_cpp import Llama
|
||||
from langchain.llms.base import LLM
|
||||
from typing import Optional, List, Mapping, Any
|
||||
|
||||
|
||||
class LlamaLLM(LLM):
|
||||
model_path: str
|
||||
llm: Llama
|
||||
@@ -16,7 +17,7 @@ class LlamaLLM(LLM):
|
||||
def __init__(self, model_path: str, **kwargs: Any):
|
||||
model_path = model_path
|
||||
llm = Llama(model_path=model_path)
|
||||
super().__init__(model_path=model_path, llm=llm, **kwargs)
|
||||
super().__init__(model_path=model_path, llm=llm, **kwargs)
|
||||
|
||||
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
||||
response = self.llm(prompt, stop=stop or [])
|
||||
@@ -26,6 +27,7 @@ class LlamaLLM(LLM):
|
||||
def _identifying_params(self) -> Mapping[str, Any]:
|
||||
return {"model_path": self.model_path}
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-m", "--model", type=str, default="./models/...")
|
||||
args = parser.parse_args()
|
||||
@@ -34,7 +36,9 @@ args = parser.parse_args()
|
||||
llm = LlamaLLM(model_path=args.model)
|
||||
|
||||
# Basic Q&A
|
||||
answer = llm("Question: What is the capital of France? Answer: ", stop=["Question:", "\n"])
|
||||
answer = llm(
|
||||
"Question: What is the capital of France? Answer: ", stop=["Question:", "\n"]
|
||||
)
|
||||
print(f"Answer: {answer.strip()}")
|
||||
|
||||
# Using in a chain
|
||||
@@ -48,4 +52,4 @@ prompt = PromptTemplate(
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
|
||||
# Run the chain only specifying the input variable.
|
||||
print(chain.run("colorful socks"))
|
||||
print(chain.run("colorful socks"))
|
||||
|
||||
@@ -27,7 +27,15 @@ embd = embd_inp
|
||||
n = 8
|
||||
|
||||
for i in range(n):
|
||||
id = llama_cpp.llama_sample_top_p_top_k(ctx, (llama_cpp.c_int * len(embd))(*embd), n_of_tok + i, 40, 0.8, 0.2, 1.0/0.85)
|
||||
id = llama_cpp.llama_sample_top_p_top_k(
|
||||
ctx,
|
||||
(llama_cpp.c_int * len(embd))(*embd),
|
||||
n_of_tok + i,
|
||||
40,
|
||||
0.8,
|
||||
0.2,
|
||||
1.0 / 0.85,
|
||||
)
|
||||
|
||||
embd.append(id)
|
||||
|
||||
@@ -38,4 +46,4 @@ for i in range(n):
|
||||
|
||||
llama_cpp.llama_free(ctx)
|
||||
|
||||
print(prediction.decode("utf-8"))
|
||||
print(prediction.decode("utf-8"))
|
||||
|
||||
Reference in New Issue
Block a user