Write code that splits some python code into its logical parts for later use in an embedding engine for free language code search.
Be concise
Assistant
To split Python code into logical parts for an embedding engine (for code search or analysis), you can leverage the Abstract Syntax Tree (AST) module. Here is a concise example to extract the relevant parts like function definitions, class definitions, and docstrings.
class CodeSplitter(ast.NodeVisitor):
def init(self):
self.parts = []
code
User
Why not use a LLM instead? It will be more generic and flexible- try it.
Be concise
Assistant
You're right. Using a large language model (LLM) can indeed offer more flexibility in understanding and extracting logical parts from code. Here's a brief explanation of how you can use an LLM for this purpose:
Generic Parsing:An LLM can infer structure from code in ways beyond strict syntax, identifying logical blocks like functions, classes, and comments more flexibly, even in non-standard formats.
class ExampleClass:
"""This is a class docstring"""
def method(self):
"""This is a method docstring"""
pass
"""
result = split_code(code)
print(result)
This code will parse a Python source and return a list of logical parts such as function definitions, class definitions, and docstrings. You can use this to generate embeddings for each part later.
Semantic Understanding:LLMs can capture relationships between code sections, infer functionality, and associate comments or docstrings to relevant parts without being limited to a rigid AST structure.
Contextual Insights:LLMs can provide more insights into the intent behind each section, offering better summaries or classifications of code blocks.
Generic Parsing:An LLM can infer structure from code in ways beyond strict syntax, identifying logical blocks like functions, classes, and comments more flexibly, even in non-standard formats.
Semantic Understanding:LLMs can capture relationships between code sections, infer functionality, and associate comments or docstrings to relevant parts without being limited to a rigid AST structure.
Contextual Insights:LLMs can provide more insights into the intent behind each section, offering better summaries or classifications of code blocks.
For example, you'd pass a snippet of Python code to an LLM prompt like this:
pythonCopy codedef extract_code_parts(code_snippet):
prompt = f"Extract the logical parts of the following Python code and categorize them:\n\n{code_snippet}\n\nReturn a structured format of functions, classes, and docstrings."
# Call LLM API (like OpenAI GPT) and get structured output
structured_output = llm_call(prompt)
return structured_output
Example call
code = """
class Example:
def func(self):
'''Function docstring'''
pass
"""
result = extract_code_parts(code)
print(result)
This method offers more flexibility and context-awareness, allowing the engine to handle diverse and non-standard Python code. However, it requires an LLM API integration to function.
Cite this conversation
“Write code that splits some python code into its logical parts for later use in an….” Conversation with ChatGPT (version unspecified), from the ShareChat — Chatbot Conversations in the Wild dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m84vp1fprm9w1gytchxrg1qn