Skip to content

Instantly share code, notes, and snippets.

@gkorland
Created April 29, 2024 09:32
Show Gist options
  • Save gkorland/62e24e927c6910431bffef21bb6104c6 to your computer and use it in GitHub Desktop.
Save gkorland/62e24e927c6910431bffef21bb6104c6 to your computer and use it in GitHub Desktop.
from langchain_community.llms import Ollama
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import time
MODEL = "phi3"
SYSTEM_PROMPT = "You are a world class technical documentation writer."
prompt = ChatPromptTemplate.from_messages([
("system", SYSTEM_PROMPT),
("user", "{input}")
])
print(f'Ollama is loading {MODEL} model...')
llm = Ollama(model=MODEL)
output_parser = StrOutputParser()
chain = prompt | llm | output_parser
# loop to ask questions
while True:
# Read text from stdin
text = input("Enter question: ")
print(f'Asking {MODEL} a question...')
# Run query and messaure time
start = time.time()
result = chain.invoke({"input": text})
end = time.time()
# print time round to two decimal places
print("Time taken: {:.2f} seconds".format(end - start))
print(f'{MODEL} says: {result}')
# Ask if user wants to ask another question
another = input("Do you want to ask another question? (y/n): ")
if another.lower() != "y":
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment