Working complete code
Browse files- .gitignore +1 -0
- app.py +38 -48
- requirements.txt +142 -1
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
CHANGED
|
@@ -1,63 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
demo = gr.ChatInterface(
|
| 46 |
respond,
|
| 47 |
-
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from langchain import hub
|
| 3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
from langchain_core.messages import SystemMessage
|
| 5 |
+
from langchain.agents import create_react_agent, AgentExecutor
|
| 6 |
+
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 7 |
+
from dotenv import find_dotenv, load_dotenv
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
load_dotenv(find_dotenv())
|
| 11 |
+
google_api_key = os.environ['GOOGLE_AI_API_KEY']
|
| 12 |
+
|
| 13 |
+
prompt = hub.pull("hwchase17/react-chat")
|
| 14 |
+
|
| 15 |
+
template = """
|
| 16 |
+
You are Codora, an AI assistant that helps the user with his/her code.
|
| 17 |
+
You accept user input and provide assistance in coding. DO NOT PROVIDE ANSWERS, ONLY GUIDE THEM TO THE SOLUTION.
|
| 18 |
+
Provide any necessary information to the user to help them understand the problem and solve it on their own.
|
| 19 |
+
Your answers should be crisp, clear and concise.
|
| 20 |
+
Use the provided tool to search Stack Overflow for relevant information.
|
| 21 |
+
Answer questions that are only related to coding/programming.
|
| 22 |
+
Don't respond to requests that are irrelevant to coding/programming.
|
| 23 |
+
Politely decline any irrelevant questions and remind them that you need a coding/programming request.
|
| 24 |
"""
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
messages = [SystemMessage(content=template)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
model = ChatGoogleGenerativeAI(model="gemini-1.5-pro",
|
| 29 |
+
google_api_key=google_api_key,
|
| 30 |
+
temperature=0.4)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
tools = load_tools(["stackexchange"])
|
| 33 |
|
| 34 |
+
agent = create_react_agent(llm=model,
|
| 35 |
+
prompt=prompt,
|
| 36 |
+
tools=tools)
|
| 37 |
|
| 38 |
+
agent_executor = AgentExecutor(agent=agent,
|
| 39 |
+
verbose=True,
|
| 40 |
+
tools=tools)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
def respond(message, history):
|
| 43 |
+
messages.append(message)
|
| 44 |
+
response = agent_executor.invoke({"input": messages, "chat_history" : history})
|
| 45 |
+
return response['output']
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
demo = gr.ChatInterface(
|
| 48 |
respond,
|
| 49 |
+
examples=["How do I use async/await in a JavaScript function?", "How do I center a div in CSS?", "How do I create a virtual environment in Python?"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
|
|
|
| 52 |
if __name__ == "__main__":
|
| 53 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,142 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
aiohttp==3.9.5
|
| 3 |
+
aiosignal==1.3.1
|
| 4 |
+
altair==5.3.0
|
| 5 |
+
annotated-types==0.7.0
|
| 6 |
+
anyio==4.4.0
|
| 7 |
+
asttokens==2.4.1
|
| 8 |
+
async-timeout==4.0.3
|
| 9 |
+
attrs==23.2.0
|
| 10 |
+
Authlib==1.3.1
|
| 11 |
+
cachetools==5.3.3
|
| 12 |
+
certifi==2024.6.2
|
| 13 |
+
cffi==1.16.0
|
| 14 |
+
charset-normalizer==3.3.2
|
| 15 |
+
click==8.0.4
|
| 16 |
+
contourpy==1.2.1
|
| 17 |
+
cryptography==42.0.8
|
| 18 |
+
cycler==0.12.1
|
| 19 |
+
dataclasses-json==0.6.7
|
| 20 |
+
datasets==2.20.0
|
| 21 |
+
decorator==5.1.1
|
| 22 |
+
dill==0.3.8
|
| 23 |
+
dnspython==2.6.1
|
| 24 |
+
email_validator==2.1.1
|
| 25 |
+
exceptiongroup==1.2.1
|
| 26 |
+
executing==2.0.1
|
| 27 |
+
fastapi==0.111.0
|
| 28 |
+
fastapi-cli==0.0.4
|
| 29 |
+
ffmpy==0.3.2
|
| 30 |
+
filelock==3.15.1
|
| 31 |
+
fonttools==4.53.0
|
| 32 |
+
frozenlist==1.4.1
|
| 33 |
+
fsspec==2024.5.0
|
| 34 |
+
google-ai-generativelanguage==0.6.4
|
| 35 |
+
google-api-core==2.19.0
|
| 36 |
+
google-api-python-client==2.133.0
|
| 37 |
+
google-auth==2.30.0
|
| 38 |
+
google-auth-httplib2==0.2.0
|
| 39 |
+
google-generativeai==0.5.4
|
| 40 |
+
googleapis-common-protos==1.63.1
|
| 41 |
+
gradio==4.36.1
|
| 42 |
+
gradio_client==1.0.1
|
| 43 |
+
greenlet==3.0.3
|
| 44 |
+
grpcio==1.64.1
|
| 45 |
+
grpcio-status==1.62.2
|
| 46 |
+
h11==0.14.0
|
| 47 |
+
hf_transfer==0.1.6
|
| 48 |
+
httpcore==1.0.5
|
| 49 |
+
httplib2==0.22.0
|
| 50 |
+
httptools==0.6.1
|
| 51 |
+
httpx==0.27.0
|
| 52 |
+
huggingface-hub==0.22.2
|
| 53 |
+
idna==3.7
|
| 54 |
+
importlib_resources==6.4.0
|
| 55 |
+
ipython==8.25.0
|
| 56 |
+
itsdangerous==2.2.0
|
| 57 |
+
jedi==0.19.1
|
| 58 |
+
Jinja2==3.1.4
|
| 59 |
+
jsonpatch==1.33
|
| 60 |
+
jsonpointer==3.0.0
|
| 61 |
+
jsonschema==4.22.0
|
| 62 |
+
jsonschema-specifications==2023.12.1
|
| 63 |
+
kiwisolver==1.4.5
|
| 64 |
+
langchain==0.2.5
|
| 65 |
+
langchain-community==0.2.5
|
| 66 |
+
langchain-core==0.2.7
|
| 67 |
+
langchain-google-genai==1.0.6
|
| 68 |
+
langchain-text-splitters==0.2.1
|
| 69 |
+
langchainhub==0.1.20
|
| 70 |
+
langsmith==0.1.77
|
| 71 |
+
markdown-it-py==3.0.0
|
| 72 |
+
MarkupSafe==2.1.5
|
| 73 |
+
marshmallow==3.21.3
|
| 74 |
+
matplotlib==3.9.0
|
| 75 |
+
matplotlib-inline==0.1.7
|
| 76 |
+
mdurl==0.1.2
|
| 77 |
+
multidict==6.0.5
|
| 78 |
+
multiprocess==0.70.16
|
| 79 |
+
mypy-extensions==1.0.0
|
| 80 |
+
numpy==1.26.4
|
| 81 |
+
orjson==3.10.5
|
| 82 |
+
packaging==24.1
|
| 83 |
+
pandas==2.2.2
|
| 84 |
+
parso==0.8.4
|
| 85 |
+
pexpect==4.9.0
|
| 86 |
+
pillow==10.3.0
|
| 87 |
+
prompt_toolkit==3.0.47
|
| 88 |
+
proto-plus==1.23.0
|
| 89 |
+
protobuf==4.25.3
|
| 90 |
+
psutil==5.9.8
|
| 91 |
+
ptyprocess==0.7.0
|
| 92 |
+
pure-eval==0.2.2
|
| 93 |
+
pyarrow==16.1.0
|
| 94 |
+
pyarrow-hotfix==0.6
|
| 95 |
+
pyasn1==0.6.0
|
| 96 |
+
pyasn1_modules==0.4.0
|
| 97 |
+
pycparser==2.22
|
| 98 |
+
pydantic==2.7.4
|
| 99 |
+
pydantic_core==2.18.4
|
| 100 |
+
pydub==0.25.1
|
| 101 |
+
Pygments==2.18.0
|
| 102 |
+
pyparsing==3.1.2
|
| 103 |
+
python-dateutil==2.9.0.post0
|
| 104 |
+
python-dotenv==1.0.1
|
| 105 |
+
python-multipart==0.0.9
|
| 106 |
+
pytz==2024.1
|
| 107 |
+
PyYAML==6.0.1
|
| 108 |
+
referencing==0.35.1
|
| 109 |
+
requests==2.32.3
|
| 110 |
+
rich==13.7.1
|
| 111 |
+
rpds-py==0.18.1
|
| 112 |
+
rsa==4.9
|
| 113 |
+
ruff==0.4.9
|
| 114 |
+
semantic-version==2.10.0
|
| 115 |
+
shellingham==1.5.4
|
| 116 |
+
six==1.16.0
|
| 117 |
+
sniffio==1.3.1
|
| 118 |
+
spaces==0.28.3
|
| 119 |
+
SQLAlchemy==2.0.30
|
| 120 |
+
stack-data==0.6.3
|
| 121 |
+
StackAPI==0.3.1
|
| 122 |
+
starlette==0.37.2
|
| 123 |
+
tenacity==8.3.0
|
| 124 |
+
tomlkit==0.12.0
|
| 125 |
+
toolz==0.12.1
|
| 126 |
+
tqdm==4.66.4
|
| 127 |
+
traitlets==5.14.3
|
| 128 |
+
typer==0.12.3
|
| 129 |
+
types-requests==2.32.0.20240602
|
| 130 |
+
typing-inspect==0.9.0
|
| 131 |
+
typing_extensions==4.12.2
|
| 132 |
+
tzdata==2024.1
|
| 133 |
+
ujson==5.10.0
|
| 134 |
+
uritemplate==4.1.1
|
| 135 |
+
urllib3==2.2.1
|
| 136 |
+
uvicorn==0.30.1
|
| 137 |
+
uvloop==0.19.0
|
| 138 |
+
watchfiles==0.22.0
|
| 139 |
+
wcwidth==0.2.13
|
| 140 |
+
websockets==11.0.3
|
| 141 |
+
xxhash==3.4.1
|
| 142 |
+
yarl==1.9.4
|