Skip to content

Commit

Permalink
llama-cpp local
Browse files Browse the repository at this point in the history
  • Loading branch information
pabik committed Sep 30, 2023
1 parent 833e183 commit b47ecab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
35 changes: 35 additions & 0 deletions application/llm/llama_cpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from application.llm.base import BaseLLM

class LlamaCpp(BaseLLM):

def __init__(self, api_key, llm_name='/Users/pavel/Desktop/docsgpt/application/models/orca-test.bin'):
global llama
from llama_cpp import Llama

Check warning on line 7 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L7

Added line #L7 was not covered by tests

llama = Llama(model_path=llm_name)

Check warning on line 9 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L9

Added line #L9 was not covered by tests

def gen(self, model, engine, messages, stream=False, **kwargs):
context = messages[0]['content']
user_question = messages[-1]['content']
prompt = f"### Instruction \n {user_question} \n ### Context \n {context} \n ### Answer \n"

Check warning on line 14 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L12-L14

Added lines #L12 - L14 were not covered by tests

result = llama(prompt, max_tokens=150, echo=False)

Check warning on line 16 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L16

Added line #L16 was not covered by tests

# import sys
# print(result['choices'][0]['text'].split('### Answer \n')[-1], file=sys.stderr)

return result['choices'][0]['text'].split('### Answer \n')[-1]

Check warning on line 21 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L21

Added line #L21 was not covered by tests

def gen_stream(self, model, engine, messages, stream=True, **kwargs):
context = messages[0]['content']
user_question = messages[-1]['content']
prompt = f"### Instruction \n {user_question} \n ### Context \n {context} \n ### Answer \n"

Check warning on line 26 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L24-L26

Added lines #L24 - L26 were not covered by tests

result = llama(prompt, max_tokens=150, echo=False, stream=stream)

Check warning on line 28 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L28

Added line #L28 was not covered by tests

# import sys
# print(list(result), file=sys.stderr)

for item in result:
for choice in item['choices']:
yield choice['text']

Check warning on line 35 in application/llm/llama_cpp.py

View check run for this annotation

Codecov / codecov/patch

application/llm/llama_cpp.py#L33-L35

Added lines #L33 - L35 were not covered by tests
4 changes: 3 additions & 1 deletion application/llm/llm_creator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from application.llm.openai import OpenAILLM, AzureOpenAILLM
from application.llm.sagemaker import SagemakerAPILLM
from application.llm.huggingface import HuggingFaceLLM
from application.llm.llama_cpp import LlamaCpp



Expand All @@ -9,7 +10,8 @@ class LLMCreator:
'openai': OpenAILLM,
'azure_openai': AzureOpenAILLM,
'sagemaker': SagemakerAPILLM,
'huggingface': HuggingFaceLLM
'huggingface': HuggingFaceLLM,
'llama.cpp': LlamaCpp
}

@classmethod
Expand Down

0 comments on commit b47ecab

Please sign in to comment.