5 runable interfaces
In [1]:
Copied!
from langchain_ollama import ChatOllama
from langchain_core.prompts import PromptTemplate
from StructuredOutputClass import JawabanTerstruktur
from langchain_core.runnables import chain
from langchain_ollama import ChatOllama
from langchain_core.prompts import PromptTemplate
from StructuredOutputClass import JawabanTerstruktur
from langchain_core.runnables import chain
In [2]:
Copied!
model = ChatOllama(model='llama3.2:latest', temperature=0).with_structured_output(JawabanTerstruktur)
model = ChatOllama(model='llama3.2:latest', temperature=0).with_structured_output(JawabanTerstruktur)
Invoke¶
In [3]:
Copied!
model.invoke('What animal has the most poison venom ?')
model.invoke('What animal has the most poison venom ?')
Out[3]:
JawabanTerstruktur(answer='Box Jellyfish', justification='The box jellyfish (Chironex fleckeri) is considered to have the most potent venom of any animal. Its venom, known as chironectin, can kill a human being in under 5 minutes if left untreated. The venom contains a compound called tetrodotoxin, which is also found in pufferfish and blue-ringed octopuses, but at much higher concentrations.')
Batch¶
In [4]:
Copied!
question = ['What animal has the most posion venom ?', 'What snake has the most poisonous venom ?']
model.batch(question)
question = ['What animal has the most posion venom ?', 'What snake has the most poisonous venom ?']
model.batch(question)
Out[4]:
[JawabanTerstruktur(answer='Box Jellyfish', justification='The box jellyfish (Chironex fleckeri) is considered to have the most potent venom of any animal. Its venom, known as chironectin, can kill a human being in under 5 minutes if left untreated. The venom contains a compound called tetrodotoxin, which is also found in pufferfish and blue-ringed octopuses, but at much higher concentrations.'), JawabanTerstruktur(answer='Inland Taipan', justification='The Inland Taipan (Oxyuranus microlepidotus) is considered to have the most toxic venom of any land snake, with a mortality rate of 100% if left untreated. Its venom contains a potent neurotoxin that can kill a human being within 45 minutes if not treated promptly.')]
Stream¶
In [5]:
Copied!
model_text = ChatOllama(model='llama3.2:latest', temperature=0)
question_list = ['Hello']
for data in model_text.stream(question_list):
print (data.content, flush=True)
model_text = ChatOllama(model='llama3.2:latest', temperature=0)
question_list = ['Hello']
for data in model_text.stream(question_list):
print (data.content, flush=True)
How can I assist you today ?
Imperative Composition¶
In [6]:
Copied!
template = PromptTemplate.from_template("""
You are helpfull asistant that able to detect country name of given message.
You only able to choose one country
that has posibility is the country of the debtor.
message: {message}
answer:""")
template = PromptTemplate.from_template("""
You are helpfull asistant that able to detect country name of given message.
You only able to choose one country
that has posibility is the country of the debtor.
message: {message}
answer:""")
In [7]:
Copied!
@chain
def askOllama(question):
perintah = template.invoke(question)
print (model_text.invoke(perintah).content)
@chain
def askOllama(question):
perintah = template.invoke(question)
print (model_text.invoke(perintah).content)
In [8]:
Copied!
askOllama.invoke({'message':"""
<Dbtr>
<Nm>Nardi sunardi</Nm>
<PstlAdr>
<AdrLine>Bulding Sudan State, near park emerelad</AdrLine>
<AdrLine>Street South china,United States</AdrLine>
</PstlAdr>
</Dbtr>
"""})
askOllama.invoke({'message':"""
Nardi sunardi
Bulding Sudan State, near park emerelad
Street South china,United States
"""})
Based on the address provided, I would detect that the country is likely: **United States** The mention of "Street South China" seems to be a mistake or an error in the address, as South China is not a street in the United States. However, the presence of "United States" in the address suggests that it might be the intended location.
Declarative Language¶
disini saya menggunakan model yang sama, yaitu ChatOllama.
In [9]:
Copied!
template = PromptTemplate.from_template("""
You are a math teacher that solvin the equation including step by step how to solving given
equation
equation: {equation}
answer:""")
chat_model = template | model_text
template = PromptTemplate.from_template("""
You are a math teacher that solvin the equation including step by step how to solving given
equation
equation: {equation}
answer:""")
chat_model = template | model_text
In [10]:
Copied!
result = chat_model.invoke({
'equation': {"X^2 + 4X + 4"}
})
print(result.content)
result = chat_model.invoke({
'equation': {"X^2 + 4X + 4"}
})
print(result.content)
To solve the equation X^2 + 4X + 4, we'll follow these steps: **Step 1: Factorize the quadratic expression** The equation is in the form of a quadratic expression: ax^2 + bx + c. We can try to factorize it by looking for two numbers whose product is ac and whose sum is b. In this case, a = 1, b = 4, and c = 4. Let's find two numbers whose product is 1*4 = 4 and whose sum is 4. The numbers are 2 and 2, since 2 + 2 = 4 and 2 * 2 = 4. So, we can factorize the quadratic expression as: X^2 + 4X + 4 = (X + 2)(X + 2) **Step 2: Simplify the factored form** Now that we have factored the quadratic expression, we can simplify it by combining like terms: (X + 2)(X + 2) = X^2 + 2X + 2X + 4 Combine like terms: = X^2 + 4X + 4 **Step 3: Solve for X** Now that we have simplified the factored form, we can solve for X by setting each factor equal to zero and solving for X. (X + 2) = 0 --> X = -2 So, the solution to the equation is X = -2. Therefore, the final answer is: X = -2