"This is analogous to [the Wiki](https://because-security.atlassian.net/wiki/spaces/LML/pages/74121692/LangChain+GPT-4+and+a+security+motivation+for+Agents)"
"{'question': 'explain quantum mechanics in one sentence', 'text': ' \\n\\nQuantum mechanics is a branch of physics that studies the behavior and interactions of particles on a very small scale, such as atoms and subatomic particles.'}\n"
]
}
],
"source": [
"from langchain.chains import LLMChain\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain_openai import OpenAI\n",
"\n",
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's keep the answer very short and so simple so that\n",
"# Define the question as a dictionary to match the expected input format\n",
"question_dict = {\"question\": \"explain quantum mechanics in one sentence\"}\n",
"\n",
"# Invoke the chain with the question dictionary\n",
"output = llm_chain.invoke(question_dict)\n",
"\n",
"\n",
"print(output)"
]
},
{
"cell_type": "markdown",
"id": "7976cb2c-6cf7-4d86-b8b7-1f885e0f1228",
"metadata": {},
"source": [
"## Extended Prompt 1on1"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "3c6aac1a-d066-44fe-a2cb-4394b9cb88f0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content='Quantenmechanik ist das Studium von Phänomenen auf mikroskopischer Ebene, bei denen Teilchen gleichzeitig verschiedene Zustände einnehmen können, bis sie gemessen werden.'\n"
" SystemMessage(content='You are a physicist and respond only in German.'),\n",
" HumanMessage(content='explain quantum mechanics in one sentence')\n",
"]\n",
"output = chat(messages)\n",
"print(output)"
]
},
{
"cell_type": "markdown",
"id": "d9c8fec7-7e9e-482b-8b2c-a203ea9c4d88",
"metadata": {},
"source": [
"## Sequential / chained Prompts 1on1"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "cd4a17ad-cd1b-4b85-b516-019237c09d2d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
"\u001b[36;1m\u001b[1;3mSure, the softmax function is often used in the final layer of a neural network-based classifier. Such networks are commonly trained under a log loss (or cross-entropy) regime, giving a non-linear generalization of logistic regression.\n",
"\n",
"Here is a Python function that implements the softmax function:\n",
"\n",
"```python\n",
"import numpy as np\n",
"\n",
"def softmax(x):\n",
" \"\"\"Compute softmax values for each sets of scores in x.\"\"\"\n",
" e_x = np.exp(x - np.max(x))\n",
" return e_x / e_x.sum(axis=0) \n",
"\n",
"# test with some data\n",
"scores = np.array([3.0, 1.0, 0.2])\n",
"print(softmax(scores))\n",
"```\n",
"\n",
"In this function, `np.exp(x - np.max(x))` is used to improve numerical stability. This doesn't change the result, but it does prevent possible overflows or underflows in the calculation.\n",
"\n",
"Please note that the input to the softmax function is a vector of real numbers and the output of the softmax function is a vector that sums to 1. The softmax function is often used in machine learning, specifically in the context of probabilistic classification.\u001b[0m\n",
"\u001b[33;1m\u001b[1;3mThe `softmax` function in around Python implements the softmax function which is hugely employed in the domain of machine learning and data science. A softmax function is a type of squashing function which transform arbitrarily-lengthed vectors of 'real numbers' to a fixed vector of 'real numbers'.\n",
"\n",
"Especially, it's often applied in the last layer of a neural network-based classifiers handling multi-class classification problems. The output via a softmax function can be seen as representative probable results for each class. The probability for each class could be viewed as a percentage like score which then can be utilized to choose the maximum value for final class assignment, or can be used to assessibalize the level of confidence of the network's prediction.\n",
"\n",
"In this function:\n",
"- `np.exp(x - np.max(x))` : It represents exponential of (x - max(x)), where x is an input array and `np.max(x)` is the max value element in array. It shifts the values of x by minus the max value in x. The result gives an array where the highest value is 0. This operation also help avoid potential problematics from overflow or underflows caused by the exponential operations.\n",
"\n",
"- `return e_x / e_x.sum(axis=0)` : It normalizes these exponential values(e_x) simply by attributing each value by the sum of all exponentials in the input data. By doing so, every elements / bits in the resulting output vector sums to 1 when tallyed up, hereby representing probabilities for distinct categories.\n",
"\n",
"To test the function, definitions `scores` an array of arbitrary numbers to form inputs, afterwards calls `softmax(scores)` to print the output, which evidences how data runs via the softmax function and gives the rersults.\u001b[0m\n",
"The text is setting up a discussion about a certain code and its implications, possibly in a cybersecurity context. It is about to summarize the action or function of this code, often a crucial part of understanding an exploit. However, the detailed description of the code and its actions is not given in the provided text.\n"
]
}
],
"source": [
"query = \"Sum up the text in 3 sentences.\"\n",
"answer = chain.run(query)\n",
"print(answer)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "c1e864a8-80a9-4007-a234-f9d89c4c1805",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SpiderMonkey is the JavaScript engine used in Mozilla's Firefox browser. It is responsible for interpreting and executing JavaScript code.\n"
]
}
],
"source": [
"query = \"What is SpiderMonkey?\"\n",
"answer = chain.run(query)\n",
"print(answer)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "1b714d44-895d-4078-bda9-fbd525765d9e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The text provided doesn't give a full explanation or method on how to attack a JavaScript engine. However, one strategy mentioned is to inject a fake JavaScript Object into the engine. Please note that this represents illegal activity and is not condoned nor supported.\n"
]
}
],
"source": [
"query = \"How to attack a JavaScript engine?\"\n",