Download tools/coder_tool.py from medmac01/sonic-cyber-assistant: direct link, hf CLI and curl.
- Browser
- Download file 1.24 kB
-
https://huggingface.co/spaces/medmac01/sonic-cyber-assistant/resolve/main/tools/coder_tool.py
- Command line
-
hf download hf://spaces/medmac01/sonic-cyber-assistant/tools/coder_tool.py
-
curl -L -o coder_tool.py https://huggingface.co/spaces/medmac01/sonic-cyber-assistant/resolve/main/tools/coder_tool.py
1.24 kB
| from langchain.tools import tool | |
| from langchain_community.llms import Ollama | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv(override=True) | |
| wrn = Ollama(model="wrn", base_url=os.getenv('OLLAMA_HOST'), num_predict=512, temperature=0.2, | |
| system=""" | |
| You are a coder and you are trying to generate a code snippet based on a given prompt. | |
| The code snippet should be in the programming language that's asked for. | |
| Don't Wrap the function in a markdown code block. Return it as a text. | |
| """) | |
| class CoderTool(): | |
| def code_generation_tool(instruction: str, language: str = "python"): | |
| """The code generation tool is a tool that can generate code snippets based on a given instruction. | |
| It uses a language model to generate code snippets that are relevant to the given instruction. | |
| Parameters: | |
| - instruction: The instruction for which the code snippet should be generated. | |
| - language: The programming language in which the code snippet should be generated. Default is python. | |
| Returns: | |
| - A code snippet generated based on the given instruction. | |
| """ | |
| response = wrn.invoke(instruction) | |
| response = response.replace("```", "") | |
| return f"'{response}'" |