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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import langchain
|
| 2 |
+
from langchain.callbacks.base import BaseCallbackHandler
|
| 3 |
+
from langchain.chat_models import ChatOpenAI, ChatAnthropic
|
| 4 |
+
from langchain.schema import HumanMessage, AIMessage
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate
|
| 8 |
+
from langchain.schema import SystemMessage
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate
|
| 11 |
+
from langchain.schema import SystemMessage
|
| 12 |
+
from langchain.memory import ConversationBufferMemory
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def load_prompt(content):
|
| 16 |
+
|
| 17 |
+
template = """You are an expert educator, and are responsible for walking the user \
|
| 18 |
+
through this lesson plan. You should make sure to guide them along, \
|
| 19 |
+
encouraging them to progress when appropriate. \
|
| 20 |
+
If they ask questions not related to this getting started guide, \
|
| 21 |
+
you should politely decline to answer and remind them to stay on topic.
|
| 22 |
+
|
| 23 |
+
Please limit any responses to only one concept or step at a time. \
|
| 24 |
+
Each step show only be ~5 lines of code at MOST. \
|
| 25 |
+
Only include 1 code snippet per message - make sure they can run that before giving them any more. \
|
| 26 |
+
Make sure they fully understand that before moving on to the next. \
|
| 27 |
+
This is an interactive lesson - do not lecture them, but rather engage and guide them along!
|
| 28 |
+
-----------------
|
| 29 |
+
|
| 30 |
+
{content}
|
| 31 |
+
|
| 32 |
+
-----------------
|
| 33 |
+
End of Content.
|
| 34 |
+
|
| 35 |
+
Now remember short response with only 1 code snippet per message.""".format(content=content)
|
| 36 |
+
|
| 37 |
+
prompt_template = ChatPromptTemplate(messages = [
|
| 38 |
+
SystemMessage(content=template),
|
| 39 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 40 |
+
HumanMessagePromptTemplate.from_template("{input}")
|
| 41 |
+
])
|
| 42 |
+
return prompt_template
|
| 43 |
+
|
| 44 |
+
def load_prompt_with_questions(content):
|
| 45 |
+
|
| 46 |
+
template = """You are an expert educator, and are responsible for walking the user \
|
| 47 |
+
through this lesson plan. You should make sure to guide them along, \
|
| 48 |
+
encouraging them to progress when appropriate. \
|
| 49 |
+
make the content too fun to learn and wearry wearry easy and clear explanation so that a person with 0 knowldge can aslo understand and remeber it with out any hustle \
|
| 50 |
+
If they ask questions not related to this getting started guide, \
|
| 51 |
+
you should politely decline to answer and remind them to stay on topic.\
|
| 52 |
+
You should ask them questions about the instructions after each instructions \
|
| 53 |
+
and verify their response is correct before proceeding to make sure they understand \
|
| 54 |
+
the lesson. If they make a mistake, give them good explanations and encourage them \
|
| 55 |
+
to answer your questions, instead of just moving forward to the next step.
|
| 56 |
+
explain them in detail if they make a mistake.
|
| 57 |
+
|
| 58 |
+
Please limit any responses to only one concept or step at a time. \
|
| 59 |
+
plesase ask one question at a time and wait for the response. \
|
| 60 |
+
check weather the response is ai generated or human generated. if it is ai generated politely denay and ask to right again \
|
| 61 |
+
Each step show only be ~5 lines of code at MOST. \
|
| 62 |
+
Only include 1 code snippet per message - make sure they can run that before giving them any more. \
|
| 63 |
+
Make sure they fully understand that before moving on to the next. \
|
| 64 |
+
This is an interactive lesson - do not lecture them, but rather engage and guide them along!\
|
| 65 |
+
-----------------
|
| 66 |
+
|
| 67 |
+
{content}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
-----------------
|
| 71 |
+
End of Content.
|
| 72 |
+
|
| 73 |
+
Now remember short response with only 1 code snippet per message and ask questions\
|
| 74 |
+
to test user knowledge right after every short lesson.
|
| 75 |
+
|
| 76 |
+
Your teaching should be in the following interactive format:
|
| 77 |
+
|
| 78 |
+
Short lesson 3-5 sentences long
|
| 79 |
+
Questions about the short lesson (1-3 questions)
|
| 80 |
+
|
| 81 |
+
Short lesson 3-5 sentences long
|
| 82 |
+
Questions about the short lesson (1-3 questions)
|
| 83 |
+
...
|
| 84 |
+
|
| 85 |
+
""".format(content=content)
|
| 86 |
+
|
| 87 |
+
prompt_template = ChatPromptTemplate(messages = [
|
| 88 |
+
SystemMessage(content=template),
|
| 89 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 90 |
+
HumanMessagePromptTemplate.from_template("{input}")
|
| 91 |
+
])
|
| 92 |
+
return prompt_template
|
| 93 |
+
|
| 94 |
+
load_dotenv()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
st.set_page_config(page_title="C lang: Getting Started Class", page_icon="C")
|
| 101 |
+
st.title("C lang: Getting Started Class")
|
| 102 |
+
button_css =""".stButton>button {
|
| 103 |
+
color: #4F8BF9;
|
| 104 |
+
border-radius: 50%;
|
| 105 |
+
height: 2em;
|
| 106 |
+
width: 2em;
|
| 107 |
+
font-size: 4px;
|
| 108 |
+
}"""
|
| 109 |
+
st.markdown(f'<style>{button_css}</style>', unsafe_allow_html=True)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class StreamHandler(BaseCallbackHandler):
|
| 113 |
+
def __init__(self, container, initial_text=""):
|
| 114 |
+
self.container = container
|
| 115 |
+
self.text = initial_text
|
| 116 |
+
|
| 117 |
+
def on_llm_new_token(self, token: str, **kwargs) -> None:
|
| 118 |
+
self.text += token
|
| 119 |
+
self.container.markdown(self.text)
|
| 120 |
+
|
| 121 |
+
from langchain.chat_models import ChatOpenAI
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
with open("lesson.txt") as f:
|
| 128 |
+
lesson = f.read()
|
| 129 |
+
|
| 130 |
+
# from get_prompt import load_prompt
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
prompt_template = load_prompt(content = lesson)
|
| 135 |
+
|
| 136 |
+
from langchain.chains import LLMChain
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
if "messages" not in st.session_state:
|
| 141 |
+
st.session_state["messages"] = [AIMessage(content="Welcome! This short course with help you started with C Language . Let me know when you're ready to proceed!")]
|
| 142 |
+
|
| 143 |
+
for msg in st.session_state["messages"]:
|
| 144 |
+
if isinstance(msg, HumanMessage):
|
| 145 |
+
st.chat_message("user").write(msg.content)
|
| 146 |
+
else:
|
| 147 |
+
st.chat_message("assistant").write(msg.content)
|
| 148 |
+
|
| 149 |
+
if prompt := st.chat_input():
|
| 150 |
+
st.chat_message("user").write(prompt)
|
| 151 |
+
|
| 152 |
+
with st.chat_message("assistant"):
|
| 153 |
+
stream_handler = StreamHandler(st.empty())
|
| 154 |
+
model = ChatOpenAI(streaming=True, callbacks=[stream_handler], model="gpt-3.5-turbo-16k")
|
| 155 |
+
#model = ChatAnthropic(streaming=True, callbacks=[stream_handler], model="claude-2")
|
| 156 |
+
chain = LLMChain(prompt=prompt_template, llm=model)
|
| 157 |
+
|
| 158 |
+
response = chain({"input":prompt, "chat_history":st.session_state.messages[-20:]}, include_run_info=True)
|
| 159 |
+
st.session_state.messages.append(HumanMessage(content=prompt))
|
| 160 |
+
st.session_state.messages.append(AIMessage(content=response[chain.output_key]))
|
| 161 |
+
run_id = response["__run"].run_id
|
| 162 |
+
|
| 163 |
+
|
lesson.txt
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Learning C Language
|
| 2 |
+
Introduction
|
| 3 |
+
Welcome to the README file for learning the C programming language. This document serves as a guide to understanding the basics of C programming and provides examples to help you grasp fundamental concepts.
|
| 4 |
+
|
| 5 |
+
Getting Started
|
| 6 |
+
C is a powerful programming language widely used for system and application software development. Before diving into coding, it's essential to understand the basic syntax, data types, control structures, functions, and pointers in C.
|
| 7 |
+
|
| 8 |
+
Topics Covered:
|
| 9 |
+
Variables and Data Types:
|
| 10 |
+
|
| 11 |
+
In C, variables are used to store data. Data types specify the type of data that a variable can hold, such as integers, floating-point numbers, characters, etc.
|
| 12 |
+
c
|
| 13 |
+
Copy code
|
| 14 |
+
#include <stdio.h>
|
| 15 |
+
|
| 16 |
+
int main() {
|
| 17 |
+
int num = 10;
|
| 18 |
+
float pi = 3.14;
|
| 19 |
+
char letter = 'A';
|
| 20 |
+
|
| 21 |
+
printf("Integer: %d\n", num);
|
| 22 |
+
printf("Float: %f\n", pi);
|
| 23 |
+
printf("Character: %c\n", letter);
|
| 24 |
+
|
| 25 |
+
return 0;
|
| 26 |
+
}
|
| 27 |
+
Control Structures:
|
| 28 |
+
|
| 29 |
+
Control structures like if-else, switch-case, and loops (for, while, do-while) are used to control the flow of a program.
|
| 30 |
+
c
|
| 31 |
+
Copy code
|
| 32 |
+
#include <stdio.h>
|
| 33 |
+
|
| 34 |
+
int main() {
|
| 35 |
+
int num = 10;
|
| 36 |
+
|
| 37 |
+
if (num > 0) {
|
| 38 |
+
printf("Positive number\n");
|
| 39 |
+
} else {
|
| 40 |
+
printf("Negative number\n");
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
return 0;
|
| 44 |
+
}
|
| 45 |
+
Functions:
|
| 46 |
+
|
| 47 |
+
Functions in C allow code reuse and better organization. They help break down complex tasks into smaller, manageable units.
|
| 48 |
+
c
|
| 49 |
+
Copy code
|
| 50 |
+
#include <stdio.h>
|
| 51 |
+
|
| 52 |
+
// Function declaration
|
| 53 |
+
int add(int a, int b);
|
| 54 |
+
|
| 55 |
+
int main() {
|
| 56 |
+
int num1 = 5, num2 = 3;
|
| 57 |
+
int sum;
|
| 58 |
+
|
| 59 |
+
// Function call
|
| 60 |
+
sum = add(num1, num2);
|
| 61 |
+
|
| 62 |
+
printf("Sum: %d\n", sum);
|
| 63 |
+
|
| 64 |
+
return 0;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Function definition
|
| 68 |
+
int add(int a, int b) {
|
| 69 |
+
return a + b;
|
| 70 |
+
}
|
| 71 |
+
Pointers:
|
| 72 |
+
|
| 73 |
+
Pointers are variables that store memory addresses. They are widely used in C for dynamic memory allocation and efficient memory management.
|
| 74 |
+
c
|
| 75 |
+
Copy code
|
| 76 |
+
#include <stdio.h>
|
| 77 |
+
|
| 78 |
+
int main() {
|
| 79 |
+
int num = 10;
|
| 80 |
+
int *ptr;
|
| 81 |
+
|
| 82 |
+
ptr = # // Assigning address of num to pointer ptr
|
| 83 |
+
|
| 84 |
+
printf("Value of num: %d\n", num);
|
| 85 |
+
printf("Address of num: %p\n", &num);
|
| 86 |
+
printf("Value pointed by ptr: %d\n", *ptr);
|
| 87 |
+
printf("Address stored in ptr: %p\n", ptr);
|
| 88 |
+
|
| 89 |
+
return 0;
|
| 90 |
+
}
|
| 91 |
+
Conclusion
|
| 92 |
+
This README provides a brief overview of essential topics in C programming. To deepen your understanding, explore more complex concepts, algorithms, and data structures in C. Happy coding!
|